删除Delphi中的重复列表视图
问题描述:
我想删除我的列表视图中的重复项。删除Delphi中的重复列表视图
此功能:
procedure RemoveDuplicates(const LV:TbsSkinListView);
var
i,j: Integer;
begin
LV.Items.BeginUpdate;
LV.SortType := stText;
try
for i := 0 to LV.Items.Count-1 do
begin
for j:=i+1 to LV.Items.Count-1 do
begin
if SameText(LV.Items[i].SubItems[0], LV.Items[j].SubItems[0]) and
SameText(LV.Items[i].SubItems[1], LV.Items[j].SubItems[1]) and
SameText(LV.Items[i].SubItems[2], LV.Items[j].SubItems[2]) and
SameText(LV.Items[i].SubItems[3], LV.Items[j].SubItems[3]) then
LV.Items.Delete(j);
end;
end;
finally
LV.SortType := stNone;
LV.Items.EndUpdate;
end;
ShowMessage('Deleted');
end;
不会删除重复项。它有什么问题?
答
在猜测,因为你没有提到什么错误,我会认为这是与事实,i
和j
无效于做你,因为你是UP计数删除了一个项目之后。
一个更好的想法是将计算下代替:
procedure RemoveDuplicates(const LV:TbsSkinListView);
var
i,j: Integer;
begin
LV.Items.BeginUpdate;
LV.SortType := stText;
try
for i := LV.Items.Count-1 downto 0 do // <- this loop now counts _down_
begin
for j:= LV.Items.Count-1 downto i+1 do // <- this loop now counts _down_
begin
if SameText(LV.Items[i].SubItems[0], LV.Items[j].SubItems[0]) and
SameText(LV.Items[i].SubItems[1], LV.Items[j].SubItems[1]) and
SameText(LV.Items[i].SubItems[2], LV.Items[j].SubItems[2]) and
SameText(LV.Items[i].SubItems[3], LV.Items[j].SubItems[3]) then
LV.Items.Delete(j);
end;
end;
finally
LV.SortType := stNone;
LV.Items.EndUpdate;
end;
ShowMessage('Deleted');
end;
+0
谢谢它的工作:),我做错了,由顶部到下来 再次感谢:) – radick 2010-04-16 03:59:21
答
在Delphi IMO要做到这一点,最简单的办法是做这样....
- 创建您的匹配性判据 即创建一个 hashkey功能添加所有你想要的文字 匹配在一起,然后散列它。
- 使用hash作为一个关键的 列表
- 浏览您的ListView和删除 不在列表中的任何项目。
伪码....(未经测试)
var
hash : integer;
i : integer;
list : SomeGenericList; // some generic list to contain your hash.
Begin
list = SomeGenericList.Create;
for i := pred(lv.Items.Count) downto 0
begin
hash := GetHashValue(lv.items[i]);
if List.Contains(hash)
begin
lv.Items.Delete(i);
end else
begin
List.Add(hash);
end;
end;
list.free;
end;
耻辱德尔福没有LINQ的类型的功能,这将是使用一个非常简单的任务。
它正在做什么,你不希望它做什么? – Nat 2010-04-16 03:43:59
我的意思是它没有删除dublicates先生 – radick 2010-04-16 03:51:15