在 Delphi XE5 中使用 multi TList 的方式

Using way multi TList in Delphi XE5

我想在 Delphi 中使用多个 TList。例如:

var
 temp1List : TList;
 temp2List : TList;
begin
 temp1List := TList.Create;
 temp2List := TList.Create;
 temp1List.add(temp2List);
end;

我认为这是不正确的,因为 TList 接受参数作为 Pointer 值。

有没有办法使用 multi TList

改为查看通用 TList<T>,例如:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TList<System.Classes.TList>.Create;
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free;
  temp2List.Free;
end;

或者,由于 TList 是 class 类型,您可以改用 TObjectList<T>,并利用它的 OwnsObjects 特性:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TObjectList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TObjectList<System.Classes.TList>.Create; // takes Ownership by default
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free; // will free temp2List for you
end;