The first comment, is that it would be preferable to factor this into a dedicated method, like areReversed(int[] array1, int[] array2)
. It would clarify the intention of the code. And, as it turns out, this will point out some improvements about the method itself.
It is currently modifying in-place the first array, in order to reverse it. If you now consider the code to be in a method, you could have
int[] array1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] array2 = new int[] { 6, 5, 4, 3, 2, 1 };
areReversed(array1, array2); // OK, prints that they are reversed versions
// some code
areReversed(array1, array2); // What? Now it prints that they aren't reversed?!
Calling the method a second time with the same arguments would not have the same result. This would be very surprising, especially so that one doesn't expect that method to behave in such a way. It is always preferable to adhere to the principle of least astonishment.
We want to know whether the two arrays are reversed, and we may want to do more than printing a String
if it is the case. Make the method return a boolean
that the calling code can use.
The insight is that you do not need to reverse an array, and determine whether the two are equal, for this. First of all, if they don't have the same length, there is no need to do anything, it is already known for sure that the result is false
. Then, you can directly compare the arrays, by traversing one in a given direction and the other one in the opposite direction.
B A C B A C B A C
| | |
----x---- x ----x----
| | |
C A B C A B C A B
Step 1 Step 2 Step 3
The upper part goes from left to right, and it compares elements with the lower part going from right to left. If there's a mismatch at any point, we know the arrays are not reverse of each other. And since we know they have the same length, all element of both arrays will have been considered.
A simple code to do this would be
public boolean areReversed(int[] array1, int[] array2) {
if (array1.length != array2.length) {
return false;
}
int length = array1.length;
for (int i = 0; i < length; i++) {
if (array1[i] != array2[length - i - 1]) {
return false;
}
}
return true;
}
That you can later use with:
if (areReversed(array, tempArray)) {
System.out.println("yep,reversed");
} else {
System.out.println("nope,not reversed");
}
You'll note that this implementation somewhat takes care of the concern you had with the code: "breaking the loop and setting a boolean value". In fact, there is no boolean variable: we return directly from the method once we know for sure what to return. The return
inside the loop is really for optimization purposes; once a mismatch is encountered, there is no need to process further the arrays, since it is known that the result can only be false
at that point. This is also what the initial break
was doing.
Small final note. Instead of
int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
int[] tempArray = new int[] { 6, 5, 4, 3, 2, 1 };
you could just have
int[] array = { 1, 2, 3, 4, 5, 6 };
int[] tempArray = { 6, 5, 4, 3, 2, 1 };
which is a bit more concise, and makes the array declaration a tad more readable.
ArrayUtils.reverse()
? \$\endgroup\$ArrayUtils.reverse(int[] array)
\$\endgroup\$