强烈建议采用排序后的TStringList——它采用二分法查找,时间复杂度最小!
var
SL:TStringList;
procedure TForm1.Button1Click(Sender: TObject);
var
i,n,m:Integer;
Value:String;
T
Word;
begin
T:=GetTickCount;
with SL do
for i:=1 to 10000 do
begin
n:=IndexOf(IntToStr(i));
if n>=0 then
begin
m:=Integer(Objects[n])
// original index is here
Value:=VALUES[m]
// Get Value !
end;
end;
Caption:=IntToStr(GetTickCount-T);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:Integer;
begin
SL:=TStringList.Create;
SL.Sorted:=true
//Here *****************
for i:=1 to 10000 do
SL.AddObject(IntToStr(Random(100000)),TObject(i))
//将原始顺序信息存入Objects数组
Caption:=IntToStr(SL.Count)
//由于排除了重复值,实际的字符串数量要小一些...
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SL.Free;
end;