详测 Generics Collections TList (3): Insert、Delete、Remove、Extract


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses Generics.Collections;



procedure TForm1.Button1Click(Sender: TObject);

var

  List: TList<Integer>;

  i: Integer;

  str: string;

begin

  List := TList<Integer>.Create();

  List.Add(111);

  List.Add(222);

  List.Add(333);



  List.Insert(0, 444);

  List.Insert(2, 555);



  str := '';

  for i in List do str := str + UIntToStr(i) + ' '; {444 111 555 222 333 }

  ShowMessage(str);



  List.Delete(0);

  List.Delete(List.Count-1);



  str := '';

  for i in List do str := str + UIntToStr(i) + ' '; {111 555 222 }

  ShowMessage(str);



  List.Remove(555); {删除指定元素}



  str := '';

  for i in List do str := str + UIntToStr(i) + ' '; {111 222 }

  ShowMessage(str);



  List.Extract(222); {提取指定元素}



  str := '';

  for i in List do str := str + UIntToStr(i) + ' '; {111 }

  ShowMessage(str);

  

  List.Free;

end;



end.


 
   

你可能感兴趣的:(Collections)