(看来要换个思路发问才能解决问题)两个stringlist合拼的问题(50)

  • 主题发起人 主题发起人
  • 开始时间 开始时间

Unregistered / Unconfirmed
GUEST, unregistred user!
有3个Tstringlist 分别是 Tstringlist_A,Tstringlist_B,Tstringlist_C想把 Tstringlist_A和Tstringlist_B合拼了再放到Tstringlist_C里面.应该怎样写语句运行速度快一些,(因为程序里有for循环语句频繁进行Tstringlist合拼运算,而且Tstringlist的容量也很大,所以重点考虑运行速度)谢谢大家帮忙.
 
...implementation{$R *.dfm}type PSTNode=^TSTNode; TSTNode=record Code:string; Name:string; Date:string; Qty:Integer;end;var FFieldName:string;procedure DelList(AList:TStrings);var obj:Pointer;begin while AList.Count>0 do begin obj:=AList.Objects[0]; Dispose(obj); AList.Delete(0); end;end;function ListCompare(List: TStringList; Index1, Index2: Integer): Integer;begin result:=0; if FFieldName='Qty' then result:=PSTNode(List.Objects[index1])^.Qty-PSTNode(List.Objects[index2])^.Qty else if FFieldName='Code' then result:=StrComp(PChar(PSTNode(List.Objects[index1])^.Code), PChar(PSTNode(List.Objects[index2])^.Code)) else if FFieldName='Name' then result:=StrComp(PChar(PSTNode(List.Objects[index1])^.Name), PChar(PSTNode(List.Objects[index2])^.Name)) else if FFieldName='Date' then result:=StrComp(PChar(PSTNode(List.Objects[index1])^.Date), PChar(PSTNode(List.Objects[index2])^.Date))end;procedure TForm1.btnSortClick(Sender: TObject);var stList:TStringList;begin stList:=TStringList.Create; try stList.Assign(ListBox1.Items); FFieldName:=cbSortBy.Text; stList.CustomSort(ListCompare); ListBox1.Items.Assign(stList); finally stList.Free; end;end;procedure TForm1.btnCreateClick(Sender: TObject);var STNode:PSTNode;begin DelList(ListBox1.Items); New(STNode); STNode.Code:='No.1'; STNode.Name:='苹果'; STNode.Date:='2009-0820'; STNode.Qty:=358; ListBox1.Items.AddObject(STNode.Code+' '+ STNode.Name+' '+STNode.Date+' '+ inttostr(STNode.Qty),TObject(STNode)); New(STNode); STNode.Code:='No.1'; STNode.Name:='苹果'; STNode.Date:='2009-0821'; STNode.Qty:=223; ListBox1.Items.AddObject(STNode.Code+' '+ STNode.Name+' '+STNode.Date+' '+ inttostr(STNode.Qty),TObject(STNode)); New(STNode); STNode.Code:='No.2'; STNode.Name:='西瓜'; STNode.Date:='2009-0820'; STNode.Qty:=23; ListBox1.Items.AddObject(STNode.Code+' '+ STNode.Name+' '+STNode.Date+' '+ inttostr(STNode.Qty),TObject(STNode)); New(STNode); STNode.Code:='No.3'; STNode.Name:='葡萄'; STNode.Date:='2009-0819'; STNode.Qty:=553; ListBox1.Items.AddObject(STNode.Code+' '+ STNode.Name+' '+STNode.Date+' '+ inttostr(STNode.Qty),TObject(STNode)); New(STNode); STNode.Code:='No.3'; STNode.Name:='葡萄'; STNode.Date:='2009-0820'; STNode.Qty:=662; ListBox1.Items.AddObject(STNode.Code+' '+ STNode.Name+' '+STNode.Date+' '+ inttostr(STNode.Qty),TObject(STNode));end;procedure TForm1.FormDestroy(Sender: TObject);begin DelList(ListBox1.Items);end;
 
Tstringlist_C.text := Tstringlist_A.text + Tstringlist_B.text;
 
没想到啊,没想到啊,用那么简单的语句就解决问题了.向你的智慧致敬.
 
多人接受答案了。
 
后退
顶部