TList的动态TList

发布于 2025-01-06 11:26:39 字数 965 浏览 3 评论 0原文

我有这个问题:我可以将列表添加到一个列表吗?我已经尝试过,但主列表总是返回一个列表,并且不明白我错在哪里。 结构是这样的:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぺ禁宫浮华殁 2025-01-13 11:26:39

您的 make sub list 函数可能是错误的,或者您可能对您的命名约定感到困惑,这些命名约定不以任何方式遵循通常​​的 Delphi/Pascal 类型命名约定。

不过,下面的代码可以工作。

请注意,TList 是一个对象,因此如果您想创建 TList 列表,请务必使用 TObjectList 而不是普通 TList,除非您喜欢内存泄漏。您的内部列表围绕记录类型,不需要是 TObjectList,但如果您将 StatsRecord 更改为 TStatsData 作为 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.

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.

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.
薆情海 2025-01-13 11:26:39

MainListPStatsList 的列表,因此您当然可以向其中添加 PStatsList 的实例。如果不允许,您的代码将根本无法编译或运行。

如果循环运行 N 次,则 MainList.Add 将被调用 N 次,并且 MainList.Count 将被调用是N。因此,如果 WriteLn(MainList.Count) 打印 1,那么我们只能得出 N = 1 的结论。

TList 允许重复,因此即使 MakeSubList 每次返回相同的对象,它仍然可以多次添加到主列表中。

MainList is a list of PStatsList, so you're certainly allowed to add instances of PStatsList to it. If you weren't allowed, your code would not have compiled or run at all.

If the loop runs N times, then MainList.Add will be called N times, and MainList.Count will be N. So, if WriteLn(MainList.Count) prints 1, then we can only conclude that N = 1.

TList allows duplicates, so even if MakeSubList is returning the same object each time, it can still get added to the main list multiple times.

绅士风度i 2025-01-13 11:26:39
    ..
    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
    
    ..
    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
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文