Damon Nomad
Damon Nomad

Reputation: 29

How to check if my array is not the same as the original?

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.

Upvotes: 0

Views: 60

Answers (5)

Roberto Linares
Roberto Linares

Reputation: 2225

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

Upvotes: 0

Ezequiel
Ezequiel

Reputation: 3592

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

boolean equals = Arrays.equals(array1, array2);

Upvotes: 0

Eran
Eran

Reputation: 393771

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)

Upvotes: 2

ortis
ortis

Reputation: 2223

Use the Arrays class:

if(!Arrays.equals(board1, board2))// check whether boeard1 and boeard2 contains the same elements
   System.out.print("Its full")

Upvotes: 0

brso05
brso05

Reputation: 13222

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.

Upvotes: 1

Related Questions