2

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");
}
3
  • Only option is to make a for loop or any kind of loop to check for all values. Commented Mar 2, 2014 at 1:03
  • Like this? for (int i = 0; i < 2; i++) { if (varray[i] == array1) { Console.WriteLine("varray contains array1"); } } Commented Mar 2, 2014 at 1:05
  • == array1[i] and don't use < 2 use < varray.length and check for && i < array1.length in if() Commented Mar 2, 2014 at 1:07

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is merely an example. I do not want to set the first array of the jagged array equal to the array i want to check.
@user3369658 Arrays are reference types.even if the both array contains same elements, they are pointing to different locations in the memory.So you can't use Contains in this case.Try Any and SequenceEqual as shown above
0

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

Comments

0

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

1 Comment

No problem. It will find it twice because it matched both elements 1 by 1.. you could fix that to only print message if it matched it completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.