L
lanbinvivy
Unregistered / Unconfirmed
GUEST, unregistred user!
我最初写了下面的函数
procedure DeleteSameNumber;
var
ix,iy,iz: integer;
begin
if listResult.Items.Count < 1 then
exit;
for ix := 0 to listResult.Items.Count - 1 do
for iy := 0 to listResult.Items.Count - 1 do
if (listResult.Items.Strings[iy]= listResult.Items.Strings[ix])and(ix <> iy) then
listResult.Delete(iz);
end
但执行时出错,提示是:... ...index out of bonds... ...
估计是删除一个重复记录后,变量发生变化,而循环任旧如初,所以超出边界。我不知道我这个推测对不对。于是改写为下面的代码:
procedure TfrmSmartVouch.DeleteSameNumber;
var
ix,iy,iz: integer;
begin
if listResult.Items.Count < 1 then
exit;
for ix := 0 to listResult.Items.Count - 1 do
for iy := 0 to listResult.Items.Count - 1 do
if (listResult.Items.Strings[iy]= listResult.Items.Strings[ix])and(ix <> iy) then
listResult.Items.Strings[iy] := 'del'+inttostr(ix)+inttostr(iy);
for iz := listResult.Items.Count - 1 downto 0 do
if Copy(listResult.Items.Strings[iz],1,3) = 'del' then
listResult.Delete(iz);
end
但这段代码运行效率极低,有没有高手指出症结所在并提供一段高明些的代码呀?
小弟先谢谢您了。
procedure DeleteSameNumber;
var
ix,iy,iz: integer;
begin
if listResult.Items.Count < 1 then
exit;
for ix := 0 to listResult.Items.Count - 1 do
for iy := 0 to listResult.Items.Count - 1 do
if (listResult.Items.Strings[iy]= listResult.Items.Strings[ix])and(ix <> iy) then
listResult.Delete(iz);
end
但执行时出错,提示是:... ...index out of bonds... ...
估计是删除一个重复记录后,变量发生变化,而循环任旧如初,所以超出边界。我不知道我这个推测对不对。于是改写为下面的代码:
procedure TfrmSmartVouch.DeleteSameNumber;
var
ix,iy,iz: integer;
begin
if listResult.Items.Count < 1 then
exit;
for ix := 0 to listResult.Items.Count - 1 do
for iy := 0 to listResult.Items.Count - 1 do
if (listResult.Items.Strings[iy]= listResult.Items.Strings[ix])and(ix <> iy) then
listResult.Items.Strings[iy] := 'del'+inttostr(ix)+inttostr(iy);
for iz := listResult.Items.Count - 1 downto 0 do
if Copy(listResult.Items.Strings[iz],1,3) = 'del' then
listResult.Delete(iz);
end
但这段代码运行效率极低,有没有高手指出症结所在并提供一段高明些的代码呀?
小弟先谢谢您了。