您的位置:首页 > 编程语言 > Delphi

Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

2015-06-27 11:52 866 查看
Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持.

Generics.Collections 包含了以下实用类:
TList<T>
TQueue<T>
TStack<T>
TDictionary<TKey,TValue>
TObjectList<T>
TObjectQueue<T>
TObjectStack<T>
TObjectDictionary<TKey,TValue>

有了以上泛型的容器, 恐怕 Classes.TList 和 Contnrs 单元下的 TObjectList 等系列容器也就只为兼容存在了.

Generics.Collections.TList<T> 既然是泛型的, 那肯定应该容得下字符串列表, 本例就依此测试吧.

如果你对泛型不了解, 应先看看: /article/6946270.html

本例效果图:



代码文件:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Generics.Collections; {Delphi 2009 新增的泛型容器单元}

var
List: TList<string>;  {定义一个泛型 TList 类, 这指定了要用于 string}
str: string = 'Test';

{建立}
procedure TForm1.FormCreate(Sender: TObject);
begin
List := TList<string>.Create;

Memo1.Clear;
Edit1.Text := str;
Button1.Caption := Button1.Caption + ' 显示';
Button2.Caption := Button2.Caption + ' 添加';
Button3.Caption := Button3.Caption + ' 插入';
Button4.Caption := Button4.Caption + ' 删除1';
Button5.Caption := Button5.Caption + ' 删除2';
Button6.Caption := Button6.Caption + ' 查找';
Button7.Caption := Button7.Caption + ' 排序';
Button8.Caption := Button8.Caption + ' 翻转';
Button9.Caption := Button9.Caption + ' 清空';
Button10.Caption := Button10.Caption + ' 添加数组';
end;

{释放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
List.Free;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
if Edit1.Text <> '' then str := Edit1.Text;
end;

{显示}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
Memo1.Clear;
for i := 0 to List.Count - 1 do
Memo1.Lines.Add(List[i]);  {List[i] = List.Item[i]}
Text := Format('当前总数: %d', [List.Count]);
end;

{添加}
procedure TForm1.Button2Click(Sender: TObject);
begin
List.Add(str);
Button1.Click; {刷新显示}
end;

{插入, 譬如插入在最前面}
procedure TForm1.Button3Click(Sender: TObject);
begin
List.Insert(0, str);
Button1.Click;
end;

{根据序号删除, 譬如删除第一个数据}
procedure TForm1.Button4Click(Sender: TObject);
begin
List.RemoveAt(0);
Button1.Click;
end;

{根据内容删除, 譬如删除第一个数据}
procedure TForm1.Button5Click(Sender: TObject);
var
s: string;
begin
s := List[0];
List.Remove(s);
Button1.Click;
end;

{查找}
procedure TForm1.Button6Click(Sender: TObject);
var
n: Integer;
begin
n := List.IndexOf(str); {LastIndexOf 是从后面找; 也可用 List.Contains(str) 判断是否包含 str}
if n = -1 then
ShowMessage('没找到')
else
ShowMessageFmt('%s 是第 %d 个', [str, n+1]);
end;

{排序}
procedure TForm1.Button7Click(Sender: TObject);
begin
List.Sort;
Button1.Click;
end;

{翻转}
procedure TForm1.Button8Click(Sender: TObject);
begin
List.Reverse;
Button1.Click;
end;

{清空}
procedure TForm1.Button9Click(Sender: TObject);
begin
List.Clear;
Button1.Click;
end;

{添加数组}
procedure TForm1.Button10Click(Sender: TObject);
const
arr: array[0..2] of string = ('CodeGear', 'Delphi', '2009');
begin
List.Add('Embarcadero');
List.AddRange(arr);
Button1.Click;
end;

end.


窗体文件:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 201
ClientWidth = 349
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 0
Top = 0
Width = 121
Height = 201
Align = alLeft
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 0
ExplicitHeight = 206
end
object Button1: TButton
Left = 136
Top = 13
Width = 81
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 127
Top = 44
Width = 100
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 127
Top = 75
Width = 100
Height = 25
Caption = 'Button3'
TabOrder = 3
OnClick = Button3Click
end
object Button4: TButton
Left = 127
Top = 106
Width = 100
Height = 25
Caption = 'Button4'
TabOrder = 4
OnClick = Button4Click
end
object Button5: TButton
Left = 127
Top = 137
Width = 100
Height = 25
Caption = 'Button5'
TabOrder = 5
OnClick = Button5Click
end
object Button6: TButton
Left = 240
Top = 44
Width = 100
Height = 25
Caption = 'Button6'
TabOrder = 6
OnClick = Button6Click
end
object Button7: TButton
Left = 240
Top = 75
Width = 100
Height = 25
Caption = 'Button7'
TabOrder = 7
OnClick = Button7Click
end
object Button8: TButton
Left = 240
Top = 106
Width = 100
Height = 25
Caption = 'Button8'
TabOrder = 8
OnClick = Button8Click
end
object Button9: TButton
Left = 240
Top = 137
Width = 100
Height = 25
Caption = 'Button9'
TabOrder = 9
OnClick = Button9Click
end
object Edit1: TEdit
Left = 240
Top = 15
Width = 100
Height = 21
TabOrder = 10
Text = 'Edit1'
OnChange = Edit1Change
end
object Button10: TButton
Left = 127
Top = 168
Width = 214
Height = 25
Caption = 'Button10'
TabOrder = 11
OnClick = Button10Click
end
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: