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?

6
  • What language is this? Commented Aug 11, 2015 at 18:00
  • The language is Java, sorry. Commented Aug 11, 2015 at 18:08
  • You do the same thing, if you are testing identity. Just make num be int[] and make array be int[][]. Commented Aug 11, 2015 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. Commented Aug 11, 2015 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. Commented Aug 11, 2015 at 18:16

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Better use collection, there things are more simpler)

Comments

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

Comments

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.