TList

unit Unit1;

interface

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

type
t1 = class
a: string;
b: string;

end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure FreeList(List: TList);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//释放类List中的对象
procedure TForm1.FreeList(List: TList);
var
I: Integer;
begin
for I := 0 to List.Count -1 do
begin
if Assigned(TObject(List.Items[I])) then
TObject(List.Items[I]).Free;
end;
List.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//
end;

procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
tt: t1;
List1, List2: TList;
begin
List1 := TList.Create;
List2 := TList.Create;
try
for I := 0 to 3 do
begin
tt := t1.Create;
tt.a := 'a ' + IntToStr(I);
tt.b := 'b ' + IntToStr(I);
List1.Add(tt);
end;
List2.Assign(List1);
for I := 0 to list2.Count -1 do
begin
ShowMessage(t1(list2.Items[I]).a);
end;
finally
FreeList(List1);
end;
end;

你可能感兴趣的:(list)