gpt4 book ai didi

delphi - 在Delphi XE5中使用方式多TList

转载 作者:行者123 更新时间:2023-12-03 19:39:22 30 4
gpt4 key购买 nike

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

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


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

有办法使用多重 TList吗?

最佳答案

看看Generic 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;

关于delphi - 在Delphi XE5中使用方式多TList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913990/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com