简体   繁体   中英

Check for Array in Jagged Array

I am trying to search an array in a jagged array.

The following code does not work. What is my mistake here?

int[] array1 = { 1, 2 };

int[][] varray = new int[2][];

varray[0] = new int[] { 1, 2 };
varray[1] = new int[] { 3, 4 };

if (varray.Contains(array1))
{
    Console.WriteLine("varray contains array1");
}

You can try this:

if (varray.Any(x => x.SequenceEqual(array1)))
{
    Console.WriteLine("varray contains array1");
}

Your array1 and varray[0] points to different locations in the memory therefore varray[0] == array1 will return false .

So if you do this:

varray[0] = array1;

Then Contains will return true .This is the only way to get true from Contains (without implementing a custom comparer) because even if the two array contains same elements, they are pointing to different locations in the memory.Instead try to use SequenceEqual , it will return true if the two array contains same elements in the same order .

The default equality comparer for arrays in C# will only find two arrays to be equal if they both have the same reference. If you replace your declaration of varray[0] with this:

varray[0] = array1;

You'll now find that the contains check returns true. Though there are a few ways to deal with this, one particularly clean one is to use the contains overload which allows you to specify a custom equality comparer. Then you would write

(varray.Contains(array1, new SequenceEqualityComparer()))

And your equality comparer class would be:

public class SequenceEqualityComparer : IEqualityComparer<IEnumerable<int>>
{
    public bool Equals(IEnumerable<int> x, IEnumerable<int> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(IEnumerable<int> obj)
    {
        return obj.GetHashCode();
    }
}

Here is a example how to do it not fancy like Demo on this URL https://ideone.com/rsUxTn

using System.IO;
using System;

class Program
{
    static void Main()
    {

        int[] array1 = { 1, 2 };

        int[][] varray = new int[4][];

        varray[0] = new int[] { 1, 2 };
        varray[1] = new int[] { 3, 4 };
        varray[2] = new int[] { 3, 4, 1 };
        varray[3] = new int[] { 3, 4, 9, 10 };

        Console.WriteLine("varray.length = " + varray.Length);
        Console.WriteLine("varray[0].length = " + varray[0].Length);

        int k = 0;
        for (int i = 0; i < varray.Length; i++) { 
            Console.WriteLine(" i = " + i);
            for(int j = 0; j < varray[i].Length; j++) {
                if (j < array1.Length && varray[i][j] == array1[k++]) {                                                                      
                    Console.WriteLine(" j = " + j);
                    Console.WriteLine("varray contains array1"); 
                }
            }
            k = 0;
        } 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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