且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从列表复制子列表

更新时间:2023-12-04 20:35:58

您正在创建的新实例MySubList,然后几乎总是用指向现有列表的指针覆盖它,然后向其中添加新项目。相反,您需要将上一个列表中的项目分别复制到新列表中:

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;