Here’s the preferred method when you want to check if two arrays contain the same elements in the same order:
Arrays.equals(array1, array2);
This method iterates through both arrays and checks each element using their equals() method.
Works perfectly for:
-
One-dimensional arrays (like int
[], String[], or Object[]) -
Content-based comparison
But: It does not work correctly for multi-dimensional arrays like String[][].