百度首页 | 百度空间
 
查看文章
 
Delphi2009(Tiburon)新特性-泛型示例
2008-07-24 15:06

Delphi Generics: Great for containers and collections

delphi泛型:强大的容器和集合

声明:

TList<T> = class
  private
    FItems: array of T;
    FCount: Integer;
    procedure Grow(ACapacity: Integer);
    function GetItem(AIndex: Integer): T;
    procedure SetItem(AIndex: Integer; AValue: T);
  public
    procedure Add(const AItem: T);
    procedure AddRange(const AItems: array of T);
    procedure RemoveAt(AIndex: Integer);
    procedure Clear;
    property Item[AIndex: Integer]: T
      read GetItem write SetItem; default;
    property Count: Integer read FCount;
  end;

使用:

var
  ilist: TList<Integer>;
  slist: TList<String>;     

procedure PrintListInteger;
var
  i: Integer;
begin
  for i := 0 to ilist.Count - 1 do
    Write(ilist[i], ' ');
  Writeln;
end;         

procedure PrintListString;
var
  i: Integer;
begin
  for i := 0 to slist.Count - 1 do
    Write(slist[i], ' ');
  Writeln;
end;         

begin
  ilist := TList.Create;
  try
    ilist.AddRange([1, 2, 3]); // ['1', 'second', 'third']);
    PrintListInteger;
    ilist.RemoveAt(1);
    PrintListInteger;
    ilist.Clear;
    PrintListInteger;
  finally
    ilist.Free;
  end;
  slist := TList.Create;
  try
    slist.AddRange(['one', 'two', 'three']); // ['first', 'second', 'third']);
    PrintListString;
    slist.RemoveAt(1);
    PrintListString;
    slist.Clear;
    PrintListString;
  finally
    slist.Free;
  end;
  Readln;
end.

The Tiburon Generics.Collections unit includes: TList, TQueue, TStack, TDictionary, TObjectList, TObjectQueue, TObjectStack, and TObjectDictionary.

Tiburon generics.collections单元包括: tlist , tqueue , tstack , tdictionary , tobjectlist , tobjectqueue , tobjectstack , tobjectdictionary 。

类别:相关资讯 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu