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> 既然是泛型的, 那肯定应该容得下字符串列表, 本例就依此测试吧.
如果你对泛型不了解, 应先看看: http://www.cnblogs.com/del/archive/2008/08/14/1268258.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 := to List.Count - 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(, str);
Button1.Click;
end; {根据序号删除, 譬如删除第一个数据}
procedure TForm1.Button4Click(Sender: TObject);
begin
List.RemoveAt();
Button1.Click;
end; {根据内容删除, 譬如删除第一个数据}
procedure TForm1.Button5Click(Sender: TObject);
var
s: string;
begin
s := List[];
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 = - then
ShowMessage('没找到')
else
ShowMessageFmt('%s 是第 %d 个', [str, n+]);
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[..] of string = ('CodeGear', 'Delphi', '2009');
begin
List.Add('Embarcadero');
List.AddRange(arr);
Button1.Click;
end; end.
窗体文件:
object Form1: TForm1
Left =
Top =
Caption = 'Form1'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch =
TextHeight =
object Memo1: TMemo
Left =
Top =
Width =
Height =
Align = alLeft
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder =
ExplicitHeight =
end
object Button1: TButton
Left =
Top =
Width =
Height =
Caption = 'Button1'
TabOrder =
OnClick = Button1Click
end
object Button2: TButton
Left =
Top =
Width =
Height =
Caption = 'Button2'
TabOrder =
OnClick = Button2Click
end
object Button3: TButton
Left =
Top =
Width =
Height =
Caption = 'Button3'
TabOrder =
OnClick = Button3Click
end
object Button4: TButton
Left =
Top =
Width =
Height =
Caption = 'Button4'
TabOrder =
OnClick = Button4Click
end
object Button5: TButton
Left =
Top =
Width =
Height =
Caption = 'Button5'
TabOrder =
OnClick = Button5Click
end
object Button6: TButton
Left =
Top =
Width =
Height =
Caption = 'Button6'
TabOrder =
OnClick = Button6Click
end
object Button7: TButton
Left =
Top =
Width =
Height =
Caption = 'Button7'
TabOrder =
OnClick = Button7Click
end
object Button8: TButton
Left =
Top =
Width =
Height =
Caption = 'Button8'
TabOrder =
OnClick = Button8Click
end
object Button9: TButton
Left =
Top =
Width =
Height =
Caption = 'Button9'
TabOrder =
OnClick = Button9Click
end
object Edit1: TEdit
Left =
Top =
Width =
Height =
TabOrder =
Text = 'Edit1'
OnChange = Edit1Change
end
object Button10: TButton
Left =
Top =
Width =
Height =
Caption = 'Button10'
TabOrder =
OnClick = Button10Click
end
end