詳細測定Generics Collections TList(11):Create
1801 ワード
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections, Generics.Defaults;
var
i: Integer;
str: string;
//
procedure TForm1.Button1Click(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.Button2Click(Sender: TObject);
var
List,ListTmp: TList<Integer>;
begin
ListTmp := TList<Integer>.Create();
ListTmp.AddRange([1, 2, 3]);
List := TList<Integer>.Create(ListTmp);
str := '';
for i in List do str := str + IntToStr(i) + ' ';
ShowMessage(str); {1 2 3 }
ListTmp.Free;
List.Free;
end;
//
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([2, 3, 1]);
List.Sort; { }
str := '';
for i in List do str := str + IntToStr(i) + ' ';
ShowMessage(str); {3 2 1 }
List.Free;
end;
end.