1

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

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

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

有没有办法使用 multi TList

4

1 回答 1

3

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

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它是一个类类型,你可以使用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;
于 2016-01-21T01:55:27.933 回答