How to compare elements of two arrays in Java?

To compare elements of two arrays in Java, call the Arrays.equals method.

Arrays.equals compares elements of arrays at same positions using the equals method (or the == operator for primitive types):

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 1, 2 };

Arrays.equals(array1, array1); // true
Arrays.equals(array1, array2); // false