C# 在列表视图中添加项目时,如何使用哈希表避免重复条目?

C# 在列表视图中添加项目时,如何使用哈希表避免重复条目?,c#,.net,listview,items,redundancy,C#,.net,Listview,Items,Redundancy,在列表视图中添加项目时,如何避免冗余。。 我正在使用winforms c#.net。。 我的意思是如何比较listview1中的项目和listview2中的项目,以便在将项目从一个listview添加到另一个listview时,无法输入已在目标listview中输入的项目。。 我可以将项目从一个列表视图添加到另一个列表视图,但它也在添加重复的项目。如何消除它?正如所建议的,哈希表是阻止这种冗余的好方法。正如所建议的,哈希表是阻止这种冗余的好方法。您可以考虑以下方法: Hashtable open

在列表视图中添加项目时,如何避免冗余。。 我正在使用winforms c#.net。。 我的意思是如何比较listview1中的项目和listview2中的项目,以便在将项目从一个listview添加到另一个listview时,无法输入已在目标listview中输入的项目。。
我可以将项目从一个列表视图添加到另一个列表视图,但它也在添加重复的项目。如何消除它?

正如所建议的,哈希表是阻止这种冗余的好方法。

正如所建议的,哈希表是阻止这种冗余的好方法。

您可以考虑以下方法:

Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is 
// already in the hash table.
try
{
    openWith.Add("txt", "winword.exe");
}
catch
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}
现在,为了满足编辑后问题中的更改,您可以这样做:

if(!ListView2.Items.Contains(myListItem))
{
    ListView2.Items.Add(myListItem);
}

您也可以在

中提到类似的问题。您可以想到类似的问题:

Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is 
// already in the hash table.
try
{
    openWith.Add("txt", "winword.exe");
}
catch
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}
现在,为了满足编辑后问题中的更改,您可以这样做:

if(!ListView2.Items.Contains(myListItem))
{
    ListView2.Items.Add(myListItem);
}

您也可以在听写中引用类似的问题。。。任意数组。。所有可能的列表,只需循环抛出项/子项,将它们添加到“数组”,然后循环抛出数组以对照otehr列表进行检查

下面是一个示例,我使用它来删除按钮上的DUP,但是您可以轻松地更改代码以满足您的需要

我使用下面的按钮删除列表视图中的“DUP”,我正在搜索子项,您可以编辑代码供自己使用

使用字典和我写的一个简单的“更新”类

private void removeDupBtn_Click(object sender, EventArgs e)
    {   

        Dictionary<string, string> dict = new Dictionary<string, string>();

        int num = 0;
        while (num <= listView1.Items.Count)
        {
            if (num == listView1.Items.Count)
            {
                break;
            }

            if (dict.ContainsKey(listView1.Items[num].SubItems[1].Text).Equals(false))
            {
                dict.Add(listView1.Items[num].SubItems[1].Text, ListView1.Items[num].SubItems[0].Text);
            }     

            num++;
        }

        updateList(dict, listView1);

    }
private void removeDupBtn\u单击(对象发送方,事件参数e)
{   
Dictionary dict=新字典();
int num=0;

而(numA dictional…Any array..一个列表所有可能的,只需循环抛出项/子项,将它们添加到“array”中,然后循环抛出数组以对照otehr列表进行检查

下面是一个示例,我使用它来删除按钮上的DUP,但是您可以轻松地更改代码以满足您的需要

我使用下面的按钮删除列表视图中的“DUP”,我正在搜索子项,您可以编辑代码供自己使用

使用字典和我写的一个简单的“更新”类

private void removeDupBtn_Click(object sender, EventArgs e)
    {   

        Dictionary<string, string> dict = new Dictionary<string, string>();

        int num = 0;
        while (num <= listView1.Items.Count)
        {
            if (num == listView1.Items.Count)
            {
                break;
            }

            if (dict.ContainsKey(listView1.Items[num].SubItems[1].Text).Equals(false))
            {
                dict.Add(listView1.Items[num].SubItems[1].Text, ListView1.Items[num].SubItems[0].Text);
            }     

            num++;
        }

        updateList(dict, listView1);

    }
private void removeDupBtn\u单击(对象发送方,事件参数e)
{   
Dictionary dict=新字典();
int num=0;

while(num)我知道哈希表是解决方案,但我不知道如何使用它?你能指导我吗…?为了清楚起见,我正在添加另一个答案和一些示例。我知道哈希表是解决方案,但我不知道如何使用它?你能指导我吗…?为了清楚起见,我正在添加另一个答案和一些示例。