要引用单元 System.Generics.Collections
implementation
{$R *.dfm}
var
i: Integer;
str: string;
procedure TForm1.btn2Click(Sender: TObject);
var
List: TList<Integer>;
begin
List := TList<Integer>.Create();
List.AddRange([11, 22, 33]);
str := '';
for i in List do
str := str + IntToStr(i) + ' ';
ShowMessage(str); { 11 22 33 }
List.Free;
end;
procedure TForm1.btn3Click(Sender: TObject);
var
List: TList<string>;
item:string;
begin
List := TList<string>.Create();
List.AddRange(['123', '546', '字符串']);
str := '';
for item in List do
str := str + item + ' ';
ShowMessage(str); { 11 22 33 }
List.Free;
end;
万一博客园 泛型 网址 http://www.cnblogs.com/del/category/213198.html
//可在建立时同时指定排序器
procedure TForm1.Button3Click(Sender: TObject);
var
List: TList<Integer>;
Comparer: IComparer<Integer>;
begin
{建立排序器}
Comparer := TComparer<Integer>.Construct(
function(const n1,n2: Integer): Integer
begin
Result := n2 - n1;
end
); List := TList<Integer>.Create(Comparer);
List.AddRange([, , ]); List.Sort; {会根据建立时指定的排序器排序} str := '';
for i in List do str := str + IntToStr(i) + ' ';
ShowMessage(str); {3 2 1 } List.Free;
end;