標籤:ext text int delphi file sage 索引 phi nbsp
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}uses IniFiles; //THashedStringList 來自 IniFiles 單元var Hash: THashedStringList;{ THashedStringList 繼承自 TStringList, 只是覆蓋了 IndexOf、IndexOfName 兩個方法以增加效率; 如果注重效率而不需要太多功能, 可以使用 TStringHash, 它是直接從 TObject 繼承的數組鏈表 }//建立雜湊表procedure TForm1.FormCreate(Sender: TObject);var i: Integer;begin Hash := THashedStringList.Create; for i := 97 to 122 do begin Hash.Add(Chr(i) + ‘=‘ + IntToStr(i)); end; ShowMessage(Hash.Text);{構建結果: a=97 b=98 c=99 d=100 e=101 f=102 g=103 h=104 i=105 j=106 k=107 l=108 m=109 n=110 o=111 p=112 q=113 r=114 s=115 t=116 u=117 v=118 w=119 x=120 y=121 z=122}end;//檢索雜湊表procedure TForm1.Button1Click(Sender: TObject);var i: Integer;begin i := Hash.IndexOf(‘z=122‘); ShowMessage(IntToStr(i)); //25 i := Hash.IndexOfName(‘z‘); ShowMessage(IntToStr(i)); //25end;//基本操作procedure TForm1.Button2Click(Sender: TObject);begin Hash.Values[‘a‘] := ‘65‘; //賦值 Hash.ValueFromIndex[0] := ‘65‘; //用索引賦值 ShowMessage(Hash.Values[‘z‘]); //122, 取值 ShowMessage(Hash.ValueFromIndex[25]);//122, 用索引取值 {其他動作參加 TStringList}end;procedure TForm1.FormDestroy(Sender: TObject);begin Hash.Free;end;end.
[轉]:Delphi 中的雜湊表(1): THashedStringList