Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

delphi - Copy sublist from list

i have this full code:

program List;

{$APPTYPE CONSOLE}

{$R *.res}

uses  System.SysUtils,   
      Generics.Collections;

type   
  TMySubList = TList<Integer>;   
  TMyList = TObjectList<TMySubList>; 

var   
  iIndex1, iIndex2: Integer;   
  MyList: TMyList;   
  MySubList: TMySubList; 

begin

 try
    { TODO -oUser -cConsole Main : Insert code here }

    MyList := TMyList.Create;
    try

      for iIndex1 := 1 to 10 do
      begin
        MySubList := TList<Integer>.Create;
        if MyList.Count <> 0 then MySubList :=  MyList.Last;
        MySubList.Add(iIndex1);
        MyList.Add(MySubList);
      end;

      for iIndex1 := 0 to pred(MyList.Count) do
      begin
        for iIndex2 := 0 to pred(MyList[iIndex1].Count) do write(MyList[iIndex1][iindex2]:5);
        writeln;
      end;

    finally
      MyList.Free;
    end;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);   
  end;

  Readln;

end.

As output i should to have something as:

 1                                   // MyList[0]
 1  2                                // MyList[1]
 1  2  3                             // MyList[2]
 1  2  3  4                          // MyList[3]
 1  2  3  4  5                       // MyList[4]
 1  2  3  4  5  6                    // MyList[5]
 1  2  3  4  5  6  7                 // MyList[6]
 1  2  3  4  5  6  7  8              // MyList[7]
 1  2  3  4  5  6  7  8  9           // MyList[8]
 1  2  3  4  5  6  7  8  9  10       // MyList[9]

But i have this result:

 1  2  3  4  5  6  7  8  9  10       // MyList[0]
 1  2  3  4  5  6  7  8  9  10       // MyList[1]
 1  2  3  4  5  6  7  8  9  10       // MyList[2]
 1  2  3  4  5  6  7  8  9  10       // MyList[3]
 1  2  3  4  5  6  7  8  9  10       // MyList[4]
 1  2  3  4  5  6  7  8  9  10       // MyList[5]
 1  2  3  4  5  6  7  8  9  10       // MyList[6]
 1  2  3  4  5  6  7  8  9  10       // MyList[7]
 1  2  3  4  5  6  7  8  9  10       // MyList[8]
 1  2  3  4  5  6  7  8  9  10       // MyList[9]

With this error to end: EInvalidPointer: Invalid Pointer Operation. The code is much simple but not understand where i mistake, or what i forget to add to it, for have the output that i want. Thanks again for who help me to solve this problem, very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're creating a new instance of MySubList and then almost always overwriting it with a pointer to an existing list which you then add a new item to. Instead you need to copy the items individually from the previous list into the new list:

  for iIndex1 := 1 to 10 do
  begin
    MySubList := TList<Integer>.Create;
    if MyList.Count <> 0 then begin
      for iIndex2 := 0 to MyList.Last.Count-1 do
        MySubList.Add(MyList.Last[iIndex2]);
    end;
    MySubList.Add(iIndex1);
    MyList.Add(MySubList);
  end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...