Compare two multi-dimensional arrays in Java
Description
The following code shows how to compare two multi-dimensional arrays.
Example
/* ww w.j ava 2 s . c o m*/
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[][] ticTacToe = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { {"O", "O", "X"},
{"O", "X", "X"},
{"X", "O", "X"}};
String[][] ticTacToe3 = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println("Boards 1 and 2 are equal.");
} else {
System.out.println("Boards 1 and 2 are not equal.");
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println("Boards 1 and 3 are equal.");
} else {
System.out.println("Boards 1 and 3 are not equal.");
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »