详测 Generics Collections TList (1): Add、Clear、Count、Capacity

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<Cardinal>;
  i: Integer;
  str: string;
begin
  List := TList<Cardinal>.Create();

  {Add}
  List.Add(22);
  List.Add(33);
  List.Add(11);

  {Count、Capacity}
  ShowMessageFmt('Count: %d; Capacity: %d', [List.Count, List.Capacity]);

  str := '';
  for i in List do str := str + UIntToStr(i) + sLineBreak;
  ShowMessage(str);

  {Clear}
  List.Clear;
  ShowMessageFmt('Count: %d; Capacity: %d', [List.Count, List.Capacity]);

  List.Free;
end;

end.

原文地址:https://www.cnblogs.com/del/p/1580642.html