Question:
How can you compare the contents of two different multi dimensional arrays?
Answer:
Use Arrays.deepEquals(array1, array2)
Code:
import java.util.Arrays; class CompareArrays { public static void main (String[] args) { int[][] array1 = { {1,2}, {1,2}, {1,2} ,{1,2}, {1,2} }; int[][] array2 = { {1,2}, {1,2}, {1,2} ,{1,2}, {1,2} }; int[][] array3 = { {1,2}, {1,2}, {1,2} ,{1,2}, {1,3} }; if (Arrays.deepEquals(array1, array2)){ System.out.println("array1 and array2: same content"); } else { System.out.println("array1 and array2: different content"); } if (Arrays.deepEquals(array1, array3)){ System.out.println("array1 and array3: same content"); } else { System.out.println("array1 and array3: different content"); } } }
Output:
$ java CompareArrays array1 and array2: same content array1 and array3: different content