将类型记录的记录添加到TList<>,可以通过以下几个步骤来完成:
完整的示例代码如下:
program AddRecordToList;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections;
type
TMyRecord = record
Field1: Integer;
Field2: string;
Field3: Boolean;
end;
var
MyList: TList<TMyRecord>;
begin
try
MyList := TList<TMyRecord>.Create;
try
// Add records to the list
for var i := 1 to 10 do
begin
var NewRecord: TMyRecord;
NewRecord.Field1 := i;
NewRecord.Field2 := 'Hello ' + IntToStr(i);
NewRecord.Field3 := i mod 2 = 0;
MyList.Add(NewRecord);
end;
// Output the list
for var Record in MyList do
begin
Writeln('Field1: ', Record.Field1);
Writeln('Field2: ', Record.Field2);
Writeln('Field3: ', Record.Field3);
Writeln;
end;
finally
MyList.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
这个示例代码创建了一个TList<TMyRecord>对象,并向其中添加了10个记录。然后,它遍历列表并输出每个记录的字段值。
领取专属 10元无门槛券
手把手带您无忧上云