2016-01-21 32 views
1

我想在Delphi中使用多个TList。例如:在Delphi中使用多TList的方法XE5

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

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

有没有办法使用多TList

回答

3

看一看通用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是类类型,你可以使用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; 
+0

我应该学习通用部分..谢谢你申请! –