Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

    ..
    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
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top