【问题标题】:Compare two string array without using linq [duplicate]比较两个字符串数组而不使用 linq [重复]
【发布时间】:2012-08-23 15:58:05
【问题描述】:

可能重复:
Comparing Arrays in C#

我有两个字符串数组:

string[] a;
string[] b;

如何确定a 中有多少(以及哪些)项目在b 中不存在?因为我使用的是 .NET 2.0,所以我不能使用 linq。

【问题讨论】:

  • 您想知道计数还是不存在的项目?到目前为止您尝试了什么?
  • @freebird 我正在读取 foreach 中的一个数组,在该 foreach 中我正在读取另一个数组,然后逐个比较每个字符串。
  • 我想知道“A”中有什么不在“B”中

标签: c# .net-2.0 arrays


【解决方案1】:
List<string> result = new List<string>();
foreach (string sa in a)
{
   if (Array.IndexOf(b, sa) < 0)
      result.Add(sa);
}

int count = result.Count;

【讨论】:

  • 'System.Array' 不包含'Contains' 的定义,并且找不到接受'System.Array' 类型的第一个参数的扩展方法'Contains'(您是否缺少 using 指令还是程序集参考?)
  • 我相信这也需要 LINQ
  • 编辑了我的答案。很难记住 2.0 中的内容。我不得不避免 3 次“var”!
【解决方案2】:

将它们都转换为 List 并执行以下操作:

List<string> difference = new List<string>();
foreach(string word in a)
{
    if(!b.Contains(word))
        difference.Add(word);
}

【讨论】:

    【解决方案3】:

    我建议将您的字符串数组转换为 HashSet&lt;T&gt;s。
    请参阅 here 了解如何在 .NET 2.0 中使用 HashSet&lt;T&gt;

    然后

    如何确定 a 中有多少(以及哪些)项目不存在 b?

    --> IntersectWith 正是这样做的。

    【讨论】:

    • IntersectWith 仅 .Net 3.5+
    • @jmh_gr .Net 3.5 和 .Net 2.0 共享相同的 DLR,有关详细信息,请参阅第一个链接。
    【解决方案4】:

    试试这个:

    string[] a = ...;
    string[] b = ...;
    
    List<string> bList = new List<string>(b);
    List<string> valuesInAButNotInB = new List<string>();
    foreach (string value in a)
    {
        if (!bList.Contains(value))
            valuesInAButNotInB.Add(value);
    }
    

    【讨论】:

      【解决方案5】:

      您需要做的是将一个列表中的项目存储在一个集合中,然后从该集合中删除所有项目(如果它们在另一个集合中)。对于较大的数据集,这将比两个嵌套循环或在其中一个数组上执行大量线性搜索要快得多。

      由于 HashSet 在 2.0 中不存在,我只使用 Dictionary 并忽略这些值。这是一种 hack,但并不可怕。

      string[] a = null;
      string[] b = null;
      Dictionary<string, string> values = new Dictionary<string, string>();
      foreach (string s in a)
      {
          values.Add(s, s);
      }
      
      foreach (string s in b)
      {
          values.Remove(s);
      }
      
      foreach (string s in values.Keys)
      {
          Console.WriteLine(s);//This string is in 'a' and not in 'b'
      }
      

      【讨论】:

        【解决方案6】:

        只需枚举a 和b 中的项目,就像过去一样:

        private static void Main(string[] args)
        {
            string[] a = new string[] { "a", "b", "c", "d" };
            string[] b = new string[] { "c", "d" };
        
            foreach (string tmp in a)
            {
                bool existsInB = false;
                foreach (string tmp2 in b)
                {
                    if (tmp == tmp2)
                    {
                        existsInB = true;
                        break;
                    }
                }
        
                if (!existsInB)
                {
                    Console.WriteLine(string.Format("{0} is not in b", tmp));
                }
            }
        
            Console.ReadLine();
        }
        

        【讨论】:

          【解决方案7】:
          private List<string> CompareArray(string[]  arr1, string[] arr2)
          {
                  List<string> compareList = new List<string>();
                  //iterate throught it
                  foreach( string str in arr1 )
                  {
                      if(!arr2.Contains( str ))
                      {
                          compareList.Add(str);
                      }
                  }
                      return compareList;
           }
          

          【讨论】:

          • 这不是他想要的。他想要集合差异,而不是序列相等。两者完全不同。
          • @DJKRAZE 您已将其更改为现在设置相等,而不是序列相等(它不需要相同的顺序),但它仍然不是设置差异。它需要返回a 中不在b 中的项目集。这不会是一个布尔值。仅你方法的返回类型就告诉你这是不对的。
          • 我以为我编辑了答案.. 抱歉我刚刚更新了,我也会再次查看
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-14
          • 2013-06-24
          • 1970-01-01
          相关资源
          最近更新 更多