【问题标题】:Using way multi TList in Delphi XE5在 Delphi XE5 中使用方式多 TList
【发布时间】:2016-01-21 01:47:19
【问题描述】:

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

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

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

有没有办法使用多个TList

【问题讨论】:

    标签: delphi tlist


    【解决方案1】:

    改为查看通用 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&lt;T&gt;,并利用其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;
    

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2021-10-13
      相关资源
      最近更新 更多