简体   繁体   English

在C#中检查两个ArrayList之间的差异

[英]Check different between two ArrayList in C#

I've data in two ArrayList arrayListA and arrayListB. 我在两个ArrayList arrayListA和arrayListB中有数据。

I want to do a check if there is a difference between these two arrayLists. 我想检查这两个arrayLists之间是否有区别。

so i do this in the code : 所以我在代码中这样做:

 ArrayList diff = new ArrayList();
 foreach (string[] a in arrayListB)
 {
     if(!arrayListA.Contains(a))
     {
         diff.Add(a);
     }
 }

so my problem here when I run the program. 所以我在运行程序时遇到的问题。 All data in arrayListB is added into ArrayList diff. arrayListB中的所有数据都添加到ArrayList diff中。 It should only add the data that is only in arrayListA and not arrayListB, correct? 它应该只添加仅在arrayListA中而不是在arrayListB中的数据,对吗?

what is going wrong? 怎么了?

This is the result after i run the program. 这是我运行程序后的结果。 The listbox a is data in arrayListA, listbox B is data in arrayListB and listbox diff is data from diff. 列表框a是arrayListA中的数据,列表框B是arrayListB中的数据,列表框diff是diff中的数据。 编辑

I already enter System.Linq. 我已经输入System.Linq。

but i don't get "Where" properties for my List. 但是我的列表没有“ Where”属性。

h

Since you are using an array list of arrays of strings, the Contains method is not going to work: it uses Equals method to check for equality, but the implementation of Equals in C# arrays does not pay attention to the equality of array elements. 由于您使用的是字符串数组的数组列表,所以Contains方法不起作用:它使用Equals方法检查是否相等,但是C#数组中Equals的实现不注意数组元素的相等性。

Here is a link to the question discussing the problem of checking array equality in C#. 这是一个讨论C#中检查数组相等性问题的问题的链接

First of all, it would be easier to work with List: 首先,使用List会更容易:

List<string[]> listA = new List<string[]>();
List<string[]> listB = new List<string[]>();

Now you can use Linq to get the ones that are in A but not in B, and the ones that are in B but not in A, and combine those to get the complete difference: 现在,您可以使用Linq来获取A中但不在B中的对象,以及B中但不在A中的对象,并将它们组合起来以得到完整的差值:

using System.Linq;

...

List<string[]> diff =
    listA.Where(a => !listB.Any(a.SequenceEqual)).Union(
    listB.Where(b => !listA.Any(b.SequenceEqual))).ToList();

Translation of the code above, with simple loops and longer code is: 上面代码的翻译,包括简单的循环和更长的代码是:

private List<string[]> GetDiff(List<string[]> listA, list<string[] listB>)
{
    var diff = new List<string[]>();

    foreach (var a in listA)
    {
        bool found = false;

        foreach (var b in listB)
        {
            if (a.SequenceEqual(b))
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            diff.Add(a);
        }
    }

    foreach (var b in listB)
    {
        bool found = false;

        foreach (var a in listA)
        {
            if (b.SequenceEqual(a))
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            diff.Add(b);
        }
    }

    return diff;
}

Assume both arrayListB and contains array of strings then you can try as 假设两个arrayListB都包含字符串数组,那么您可以尝试

 ArrayList diff = new ArrayList();
 foreach (var b in arrayListB)
 {
     bool found = false;
     foreach (var a in arrayListA)
     {
        if(Enumerable.SequenceEqual(a as string[], b as string[]))
        {
          found = true
          break;
        }
     }
     if(!found)
     {
        diff.Add(b);
     }
 }

There is no built in Equal for string[] that makes Contains work the way you probably want. 没有内置的等于等于string[]可以使Contains以您可能想要的方式工作。 You need to implement custom comparison for string[] and use the other override of Contains that takes comparer as argument. 您需要实现string[]自定义比较,并使用包含比较器作为参数的Contains的其他覆盖。

As side note it would be better to use strongly typed lists to start with like List<string[]> . 附带说明一下,最好使用强类型列表以类似List<string[]>开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM