我有这个问题:因为我可以将列表添加到一个列表中?我已经尝试过了,但是主列表总是返回一个列表,并且不明白我错在哪里。 结构如下:
PCombArray = array of Integer;
PStatsRecord = record
Comb: PCombArray;
Freq: Integer;
end;
PStatsList = TList<PStatsRecord>;
TStatsList = TList<PStatsList>;
其中Comb是作为主键的字段。但是这一切都好。我将主列表定义为:
var
MainList: TStatsList;
MySubList: PStatsList;
并创建它,没有问题。用于填充子列表的过程工作;例如,我将此过程称为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;
感谢谁帮助我理解,所以我可以解决它。再次感谢。
答案 0 :(得分:3)
MainList
是PStatsList
的列表,因此您当然可以向其添加PStatsList
的实例。如果您不被允许,您的代码将无法编译或运行。
如果循环运行N
次,则MainList.Add
将被调用N
次,MainList.Count
将为N
。因此,如果WriteLn(MainList.Count)
打印1
,那么我们只能得出N = 1
。
TList
允许重复,因此即使MakeSubList
每次都返回相同的对象,它仍然可以多次添加到主列表中。
答案 1 :(得分:1)
你的make子列表函数可能有误,或者你可能会对你的命名约定感到困惑,这些约定并没有以任何方式遵循类型的通常Delphi / Pascal命名约定。
但是以下代码可以正常工作。
请注意,TList是一个Object,所以如果你想创建一个TList列表,一定要使用TObjectList而不是普通的TList,除非你喜欢内存泄漏。您的内部列表是一个记录类型,不需要是TObjectList,但如果您将StatsRecord更改为TODatsData作为TObject(类)类型,您还应该更改为TObjectList。
unit aUnit5;
interface
uses Generics.Collections;
procedure Test;
implementation
type
CombArray = array of Integer;
StatsRecord = record
Comb: CombArray;
Freq: Integer;
end;
TStatsList2 = TList<StatsRecord>;
TStatsList = TObjectList<TStatsList2>;
var
MainList: TStatsList;
MySubList: TStatsList2;
index:Integer;
procedure Test;
begin
MainList := TStatsList.Create;
try
for index := 1 to 10 do // generate N subList
begin
MySubList := TStatsList2.Create;
MainList.Add(MySubList); // add mysublist to mainlist
end;
finally
MainList.Free;
end;
end;
end.
答案 2 :(得分:0)
.. implementation type S64 = string[64]; art_ptr = ^art_rec; art_rec = record sDes: S64; rCost: real; iQty: integer; end; art_file = file of art_rec; var lCatalog: TList; procedure disp_all; var lArt: TList; pA: art_ptr; c, a: integer; begin for c:= 0 to lCatalog.Count -1 do begin lArt:= lCatalog[c]; for a:= 0 to lArt.Count -1 do begin pA:= lArt[a]; Dispose(pA); end; lArt.Clear; lArt.Free; end; lCatalog.Clear; end;// disp_all procedure TfrmMain.FormCreate(Sender: TObject); begin lCatalog:= TList.Create; end;// FormCreate procedure TfrmMain.FormDestroy(Sender: TObject); begin disp_all; lCatalog.Free; end;// FormDestroy // a normal BitButton click that adds 3 records to each art list (2) procedure TfrmMain.bbMCreate_Click(Sender: TObject); const nArt = 2; nRec = 3; var pA: art_ptr; lArt: TList; c: integer; begin // clears any previous added pointer record to art list that was added to the catalog disp_all; // creates art lists and add 'em to the catalog list for c:= 0 to nArt -1 do begin lArt:= TList.Create; lCatalog.Add(lArt); // creates records and add 'em to the art list for a:= 0 to nArt -1 do begin pA:= New(art_ptr); with pA^ do begin sDes:= Format('cat%d-art%d', [c, a]); rCost:= (c+1) * (a +1) *1.0; iQty:= (c+1) *10 + (a +1); end; lArt.Add(pA); end; end; end;// bbMCreate_Click // a normal BitButton click that reads 1 record from 1 art list procedure TfrmMain.bbMRead_Click(Sender: TObject); const c = 1; a = 2; var pA: art_ptr; lArt: TList; begin // gets art list #2 from catalog (index is 0 based. c=1) lArt:= lCatalog[c]; // gets record #3 from art list (index is 0 based. a=2) pA:= lArt[a]; // displays the record in a string grid called sgArt... at row a+1 with sgArt, pA^ do begin // col 0 contains cat index and art index, can be useful... cells[0,a+1]:= Format('\%d\%d', [c,a]); cells[1,a+1]:= sDes; cells[2,a+1]:= FormatFloat('0.00', rCost); cells[3,a+1]:= IntToStr(iQty); end; end;// bbMRead_Click