Использование способа multi TList в Delphi XE5

Я хочу использовать multi TList в Delphi. Например:

var
 temp1List : TList;
 temp2List : TList;
begin
 temp1List := TList.Create;
 temp2List := TList.Create;
 temp1List.add(temp2List);
end;

Я думаю, что это неправильно, потому что TList принимает параметр как значение Pointer.

Есть ли способ использовать мульти TList?


person Jung-soo Lee    schedule 21.01.2016    source источник


Ответы (1)


Вместо этого взгляните на Generic TList<T>, например:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TList<System.Classes.TList>.Create;
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free;
  temp2List.Free;
end;

В качестве альтернативы, поскольку TList является типом класса, вы можете использовать вместо него TObjectList<T> и воспользоваться его функцией OwnsObjects:

uses
  ..., System.Classes, System.Generics.Collections;

var
  temp1List : System.Generics.Collections.TObjectList<System.Classes.TList>;
  temp2List : System.Classes.TList;
begin
  temp1List := System.Generics.Collections.TObjectList<System.Classes.TList>.Create; // takes Ownership by default
  temp2List := System.Classes.TList.Create;
  temp1List.Add(temp2List);
  // don't forget to free them when you are done...
  temp1List.Free; // will free temp2List for you
end;
person Remy Lebeau    schedule 21.01.2016