Comparing two integer arrays in Java

From what I see you just try to see if they are equal, if this is true, just go with something like this:

boolean areEqual = Arrays.equals(arr1, arr2);

This is the standard way of doing it.

Please note that the arrays must be also sorted to be considered equal, from the JavaDoc:

Two arrays are considered equal if both arrays contain the same number
of elements, and all corresponding pairs of elements in the two arrays
are equal. In other words, two arrays are equal if they contain the
same elements in the same order.

Sorry for missing that.

Leave a Comment