【问题标题】:C# Adding a item into an arraylist if the item is not already inside that arraylist如果该项目尚未在该数组列表中,则 C# 将项目添加到该数组列表中
【发布时间】:2021-11-04 13:09:39
【问题描述】:

假设我有两个列表列表 1 和列表 2。列表 1 具有字符串“ABCD”,列表 2 具有与列表 1 相同的项目,但有一些附加项目“1A23C4D”,我想将它们组合起来一个包含所有可能值且不重复的列表。所以我希望 ABCD1234 在第三个列表中

这就是我所拥有的

        ArrayList list1 = new ArrayList();

        list1.Add("A");
        list1.Add("B");
        list1.Add("C");
        list1.Add("D");

        ArrayList list2 = new ArrayList();
        list2.Add("1");
        list2.Add("A");
        list2.Add("2");
        list2.Add("3");
        list2.Add("C");
        list2.Add("4");
        list2.Add("D");

        ArrayList list3 = new ArrayList();

        foreach(var item in list1)
        {
            list3.Add(item);
        }

        foreach (var item in list1)
        {
            foreach (var item2 in list2)
            {
                //if the item2 is not at all in the first list add it to the 3rd list
            }
        }

        Console.ReadLine();

我弄清楚这一点的原因是因为我在做其他事情,我从一个数组列表中的一个 reg 键安装了所有软件,然后从另一个 reg 键安装了第二个数组列表中的所有软件,我想创建显示的所有已安装软件的组合列表不会重复显示。除了 c# 之外,我在其他语言中看到了与此类似的内容,所以我认为我要求 c#。谢谢

【问题讨论】:

  • 你真的不应该使用ArrayList 它是泛型之前的遗留物。请改用List<string>
  • 除了使用List<string> 而不是@juharr 已经指出的ArrayList,考虑使用HashSet<string> 以提高效率.Contains()

标签: c# arraylist foreach


【解决方案1】:

您不应该使用ArrayList,它是来自.net framework 1.0(2002 年)的古老数据结构。使用 List<T> 代替 .net 框架 2.0 及更高版本(2005 年)。

您可以使用 .net framework 3.5 及更高版本(2008 年)中的 Linq 来构建具有不同项目的列表:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var list1 = new List<string> { "A", "B", "C", "D" };
            var list2 = new List<string> { "1", "A", "2", "3", "C", "4", "D" };

            var list3 = list1.Concat(list2).Distinct().ToList();

            foreach (var item in list3)
            {
                Console.WriteLine(item);
            }
        }
    }
}

还有其他一些选择。如果您只需要一个可枚举,您可以使用以下代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var list1 = new List<string> { "A", "B", "C", "D" };
            var list2 = new List<string> { "1", "A", "2", "3", "C", "4", "D" };

            var distinctItems = list1.Concat(list2).Distinct();

            foreach (var item in distinctItems)
            {
                Console.WriteLine(item);
            }
        }
    }
}

如果您想要一个具有不同项目的数据结构,您可以使用HashSet&lt;string&gt;

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var list1 = new List<string> { "A", "B", "C", "D" };
            var list2 = new List<string> { "1", "A", "2", "3", "C", "4", "D" };

            var distinctItems = new HashSet<string>(list1.Concat(list2));

            foreach (var item in distinctItems)
            {
                Console.WriteLine(item);
            }
        }
    }
}

【讨论】:

  • 在这种情况下,我会说它应该是 HashSet&lt;string&gt; 而不是 List,因为该集合是专门为排除欺骗而设计的,尽管它不会影响此答案的有效性
  • @MikeT 如果要求得到一个列表作为结果怎么办? Distinct 方法使用高效的数据结构来执行所需的操作。
  • 我的建议不是关键的改进
  • @MikeT 已编辑以包含其他选项,例如 HashSet
  • @MrClayGiovanni 没有数据结构总是比另一个更好。您应该使用更适合您需要解决的问题的数据结构。有很多关于 HashSet 和哈希表的信息,哈希表是用于实现 HashSet 的底层数据结构
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2012-02-10
  • 2015-01-02
  • 2015-06-12
  • 1970-01-01
相关资源
最近更新 更多