0

Here, I have an array in Java, lose, that has two arrays inside of it: xLose and yLose.

    int[] xLose = selectLose(buttons, xNum);
    int[] yLose = selectLose(buttons, yNum);
    int[][] lose = {xLose, yLose};

I already have a method that can check if an element is an array:

public boolean isInArray(int num, int[] array)
{
    for (int i = 0; i < array.length; i++)
    {
        if(num == array[i])
        {
            return true;
        }
    }
    return false;
}

But how do I check if an array is an element of an array of arrays? For example, is xLose in lose?

Tony Tuttle
  • 612
  • 7
  • 23
michaeljan
  • 277
  • 3
  • 12
  • What language is this? – Two-Bit Alchemist Aug 11 '15 at 18:00
  • The language is Java, sorry. – michaeljan Aug 11 '15 at 18:08
  • You do the same thing, if you are testing identity. Just make `num` be `int[]` and make `array` be `int[][]`. – dsh Aug 11 '15 at 18:09
  • num will never equal array[i] .... regardless of the language, you need to break up array[i] so that it looks at the first place holder (which is xlose) in array[i]... Right now this formulates to num == array[i] == {xLose,yLose} ... which will never be true. – Nefariis Aug 11 '15 at 18:10
  • The isInArray is checking if a number is in a one-dimensional array. Why can't num == array[i]? array[i] is an int and num is an int also. – michaeljan Aug 11 '15 at 18:16
  • possible duplicate of [Finding if an array contains all elements in another array](http://stackoverflow.com/questions/16524709/finding-if-an-array-contains-all-elements-in-another-array) – StefanHeimberg Aug 11 '15 at 18:24

3 Answers3

1
import java.util.Arrays;

public boolean isInArray(int[] sub, int[][] sup){
    for(int i=0; i<sup.length; i++)
        if(Arrays.equals(sub, sup[i]))
            return true;
    return false;
}
Tony Tuttle
  • 612
  • 7
  • 23
0

Better use collection, there things are more simpler)

Shell Scott
  • 1,679
  • 18
  • 28
0

you need to do something like this:

public boolean ArrayisInArray(int[] array1, int[][] array2)
{
    int cont=0;
    for (int i = 0; i < array2.length; i++)
    {          
      for (int j = 0; j < array2.length; i++)
      {
          if (array1[j]==array2[i][j])  
           cont++;

         if (cont == array1.length)
             return true;
      }
      cont=0;
    }
    return false;
}

this code can compare array1 with array2 row by row finding your first array in the second array