TList的动态TList
我有这个问题:我可以将列表添加到一个列表吗?我已经尝试过,但主列表总是返回一个列表,并且不明白我错在哪里。 结构是这样的:
PCombArray = array of Integer;
PStatsRecord = record
Comb: PCombArray;
Freq: Integer;
end;
PStatsList = TList<PStatsRecord>;
TStatsList = TList<PStatsList>;
其中 Comb 是作为主键的字段。但这里一切都好。我将主列表定义为:
var
MainList: TStatsList;
MySubList: PStatsList;
并创建它,没有问题。用于填充子列表的过程;对于 esample,我将此过程称为 MakeSubList 并分配一个 MySubList 列表,然后将其添加到主列表中:
MainList := TList<PStatsList>.Create;
try
MainList.Clear;
for index := 1 to N do // generate N subList
begin
...
MySubList := MakeSubList(index); // contain correct sub list, no problem here
...
MainList.Add(MySubList); // add mysublist to mainlist
end;
writeln (mainList.count); // return 1, and not N-1 sublist
finally
MainList.Free;
end;
谢谢谁帮助我理解,以便我可以解决它。再次感谢。
i have this problem: as i can add list to one list? i have tried so, but main list return always one list, and not understand where i mistake.
The structure is this:
PCombArray = array of Integer;
PStatsRecord = record
Comb: PCombArray;
Freq: Integer;
end;
PStatsList = TList<PStatsRecord>;
TStatsList = TList<PStatsList>;
Where Comb is a field that work as primary key. But here all ok. i define main list as:
var
MainList: TStatsList;
MySubList: PStatsList;
and create it, without problem. A procedure work for populate a subList; for esample i call this procedure as MakeSubList and assign a MySubList the list maked, then i add it to main list:
MainList := TList<PStatsList>.Create;
try
MainList.Clear;
for index := 1 to N do // generate N subList
begin
...
MySubList := MakeSubList(index); // contain correct sub list, no problem here
...
MainList.Add(MySubList); // add mysublist to mainlist
end;
writeln (mainList.count); // return 1, and not N-1 sublist
finally
MainList.Free;
end;
Thank who help me to understand so i can solve it. Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 make sub list 函数可能是错误的,或者您可能对您的命名约定感到困惑,这些命名约定不以任何方式遵循通常的 Delphi/Pascal 类型命名约定。
不过,下面的代码可以工作。
请注意,TList 是一个对象,因此如果您想创建 TList 列表,请务必使用 TObjectList 而不是普通 TList,除非您喜欢内存泄漏。您的内部列表围绕记录类型,不需要是 TObjectList,但如果您将 StatsRecord 更改为 TStatsData 作为 TObject(类)类型,则还应该更改为 TObjectList。
Your make sub list function might be wrong, or you might have confused yourself with your naming conventions which did not follow in any way the usual Delphi/Pascal naming conventions for types.
The following code works, though.
Note that a TList is an Object so if you want to make a List of TList be sure to use TObjectList instead of plain TList unless you like memory leaks. Your inner list is around a record type and does not need to be TObjectList, but if you changed StatsRecord to TStatsData as a TObject (class) type, you should also change to TObjectList.
MainList
是PStatsList
的列表,因此您当然可以向其中添加PStatsList
的实例。如果不允许,您的代码将根本无法编译或运行。如果循环运行
N
次,则MainList.Add
将被调用N
次,并且MainList.Count
将被调用是N
。因此,如果WriteLn(MainList.Count)
打印1
,那么我们只能得出N = 1
的结论。TList
允许重复,因此即使MakeSubList
每次返回相同的对象,它仍然可以多次添加到主列表中。MainList
is a list ofPStatsList
, so you're certainly allowed to add instances ofPStatsList
to it. If you weren't allowed, your code would not have compiled or run at all.If the loop runs
N
times, thenMainList.Add
will be calledN
times, andMainList.Count
will beN
. So, ifWriteLn(MainList.Count)
prints1
, then we can only conclude thatN = 1
.TList
allows duplicates, so even ifMakeSubList
is returning the same object each time, it can still get added to the main list multiple times.