Qusyaire Ezwan
Qusyaire Ezwan

Reputation: 279

Check different between two ArrayList in C#

I've data in two ArrayList arrayListA and arrayListB.

I want to do a check if there is a difference between these two 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. It should only add the data that is only in arrayListA and not arrayListB, correct?

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. EDIT

I already enter System.Linq.

but i don't get "Where" properties for my List.

haih

Upvotes: 1

Views: 5501

Answers (4)

Damith
Damith

Reputation: 63065

Assume both arrayListB and contains array of strings then you can try as

 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);
     }
 }

Upvotes: 1

YoryeNathan
YoryeNathan

Reputation: 14522

First of all, it would be easier to work with 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:

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;
}

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726549

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.

Here is a link to the question discussing the problem of checking array equality in C#.

Upvotes: 4

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

There is no built in Equal for string[] that makes Contains work the way you probably want. You need to implement custom comparison for string[] and use the other override of Contains that takes comparer as argument.

As side note it would be better to use strongly typed lists to start with like List<string[]>.

Upvotes: 0

Related Questions