本文整理自网络,侵删。
Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持.
Generics.Collections 包含了以下实用类:TList TQueue TStack TDictionary TObjectList TObjectQueue TObjectStack TObjectDictionary
有了以上泛型的容器, 恐怕 Classes.TList 和 Contnrs 单元下的 TObjectList 等系列容器也就只为兼容存在了.
Generics.Collections.TList 既然是泛型的, 那肯定应该容得下字符串列表, 本例就依此测试吧.
如果你对泛型不了解, 应先看看: http://www.cnblogs.com/del/archive/2008/08/14/1268258.html
本例效果图:
代码文件:unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; Button10: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Edit1Change(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button8Click(Sender: TObject); procedure Button9Click(Sender: TObject); procedure Button10Click(Sender: TObject); end;
var Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections; {Delphi 2009 新增的泛型容器单元}
var List: TList; {定义一个泛型 TList 类, 这指定了要用于 string} str: string = 'Test';
{建立}procedure TForm1.FormCreate(Sender: TObject);begin List := TList.Create;
Memo1.Clear; Edit1.Text := str; Button1.Caption := Button1.Caption + ' 显示'; Button2.Caption := Button2.Caption + ' 添加'; Button3.Caption := Button3.Caption + ' 插入'; Button4.Caption := Button4.Caption + ' 删除1'; Button5.Caption := Button5.Caption + ' 删除2'; Button6.Caption := Button6.Caption + ' 查找'; Button7.Caption := Button7.Caption + ' 排序'; Button8.Caption := Button8.Caption + ' 翻转'; Button9.Caption := Button9.Caption + ' 清空'; Button10.Caption := Button10.Caption + ' 添加数组';end;
{释放}procedure TForm1.FormDestroy(Sender: TObject);begin List.Free;end;
procedure TForm1.Edit1Change(Sender: TObject);begin if Edit1.Text <> '' then str := Edit1.Text;end;
{显示}procedure TForm1.Button1Click(Sender: TObject);var i: Integer;begin Memo1.Clear; for i := 0 to List.Count - 1 do Memo1.Lines.Add(List[i]); {List[i] = List.Item[i]} Text := Format('当前总数: %d', [List.Count]);end;
{添加}procedure TForm1.Button2Click(Sender: TObject);begin List.Add(str); Button1.Click; {刷新显示}end;
{插入, 譬如插入在最前面}procedure TForm1.Button3Click(Sender: TObject);begin List.Insert(0, str); Button1.Click;end;
{根据序号删除, 譬如删除第一个数据}procedure TForm1.Button4Click(Sender: TObject);begin List.RemoveAt(0); Button1.Click;end;
{根据内容删除, 譬如删除第一个数据}procedure TForm1.Button5Click(Sender: TObject);var s: string;begin s := List[0]; List.Remove(s); Button1.Click;end;
{查找}procedure TForm1.Button6Click(Sender: TObject);var n: Integer;begin n := List.IndexOf(str); {LastIndexOf 是从后面找; 也可用 List.Contains(str) 判断是否包含 str} if n = -1 then ShowMessage('没找到') else ShowMessageFmt('%s 是第 %d 个', [str, n+1]);end;
{排序}procedure TForm1.Button7Click(Sender: TObject);begin List.Sort; Button1.Click;end;
{翻转}procedure TForm1.Button8Click(Sender: TObject);begin List.Reverse; Button1.Click;end;
{清空}procedure TForm1.Button9Click(Sender: TObject);begin List.Clear; Button1.Click;end;
{添加数组}procedure TForm1.Button10Click(Sender: TObject);const arr: array[0..2] of string = ('CodeGear', 'Delphi', '2009');begin List.Add('Embarcadero'); List.AddRange(arr); Button1.Click;end;
end.
相关阅读 >>
Delphi try except与try finally不同之处
Delphi windows 编程[10] - wm_lbuttondown、wm_lbuttonup 和 wm_mousemove 消息
Delphi 捕捉异常 try except语句 和 try finally语句用法以及区别
更多相关阅读请进入《Delphi》频道 >>