0

IN JAVA I have an array like the fallowing:

int[] board={0,0,0}

If I change it to something like:

board[1,2,3]

I want to check if my current board is equal to the previous board:

if (board[0,0,0] != board[1,2,10]){
   System.out.print("Its full")
}

And I want to get if it's right or wrong.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • See this answer: http://stackoverflow.com/a/14897467/822870 Although your question is not very clear. What does for example `board[1,2,3]` mean? – David Balažic Oct 30 '14 at 15:00

5 Answers5

2

You'll have to create a copy of the original array in order to be able to compare it to the new state of the array.

The comparison itself can be done with

Arrays.equals(originalArray, currentArray)
Eran
  • 387,369
  • 54
  • 702
  • 768
1

You need to check the elements individually. Loop through one array comparing to values of other array:

boolean same = true;
for(int i = 0; i < board.length; i++)
{
   if(board[i] != board2[i]
   {
      same = false;
      break;
   }
}

if same is true then they are the same if not then they are not the same.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

Use the Arrays class:

if(!Arrays.equals(board1, board2))// check whether boeard1 and boeard2 contains the same elements
   System.out.print("Its full")
ortis
  • 2,203
  • 2
  • 15
  • 18
0

Try with Arrays class from http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html.

boolean equals = Arrays.equals(array1, array2);
Ezequiel
  • 3,477
  • 1
  • 20
  • 28
0

In Java, there is a utility class called Arrays, which has an equals() overloaded method that you can use to compare if two arrays have the same values in each position.

Check the Oracle Documentation about Arrays.equals()

Roberto Linares
  • 2,215
  • 3
  • 23
  • 35