How to Reverse Array in Java
If you are looking for how to reverse an array in Java, then this article is just written for you, keep reading until the end.
As we know, arrays are one of the most used data structures for storing multiple values in a single variable.
So, we are going to explore different ways to invert the order of an array’s values using Java core methods and external libraries.
Reverse Array Using Java 7
Java 7 provides several approaches to invert an array. Let’s go down the rabbit hole and take a close look at each approach.
Using Swapping Approach
To reverse the order of an array’s elements, we can simply use a traditional for loop.
First, we need to iterate over the given array. Then, we alternate the element at the index i with the element at the index arrayLength - i - 1 until we reach the middle of the array.
Now, let’s see how to implement this in Java:
@Test
public void reverseArrayUsingSwapping() {
String[] initialArray = { "A", "B", "C", "D", "E", "F" };
for (int i = 0; i < initialArray.length / 2; i++) {
String temp = initialArray[i];
initialArray[i] = initialArray[initialArray.length - i - 1];
initialArray[initialArray.length - i - 1] = temp;
}
String[] reversedArray = { "F", "E", "D", "C", "B", "A" };
assertArrayEquals(reversedArray, initialArray);
}
Using Copying Approach
Here, we will use the for loop as well, but with a different logic.
The basic idea is to use another array that acts as a copy of the initial array. However, we need to copy the elements in reverse order.
Now, let’s illustrate this using a practical example:
@Test
public void reverseArrayUsingCopying() {
String[] initialArray = { "A", "B", "C", "D", "E", "F" };
String[] result = new String[initialArray.length];
for (int i = 0; i < initialArray.length; i++) {
result[i] = initialArray[initialArray.length - i - 1];
}
String[] reversedArray = { "F", "E", "D", "C", "B", "A" };
assertArrayEquals(reversedArray, result);
}
Please note that we can’t use the enhanced for loop to iterate through an array backward.
Using Collections.reverse() Method
Collections is a utility class that comes with multiple ready-to-use static methods for operating and manipulating collections.
Among these methods, we find the reverse() method. It was introduced with the aim of reversing a collection of elements.
@Test
public void reverseArrayUsingCollections() {
String[] initialArray = { "A", "B", "C", "D", "E", "F" };
Collections.reverse(Arrays.asList(initialArray));
String[] reversedArray = { "F", "E", "D", "C", "B", "A" };
assertArrayEquals(reversedArray, initialArray);
}
Please note that the method accepts a list and not an array. This is why we used the Arrays.asList() method.
Array in Reverse Order Using Java 8
The Java 8 stream API provides another convenient way to reverse the order of the elements in a particular array.
So, let’s exemplify this using a test case:
@Test
public void reverseArrayUsingStreamApi() {
int[] initialArray = { 1, 9, 5, 8, 3, 2 };
int[] result = IntStream.rangeClosed(1, initialArray.length)
.map(i -> initialArray[initialArray.length - i])
.toArray();
int[] reversedArray = { 2, 3, 8, 5, 9, 1 };
assertArrayEquals(reversedArray, result);
}
As we can see, we used map to swap the elements. Then, we called the terminal operation toArray() to return the reversed array.
Using Guava API
Guava is a library provided by Google. It comes with a set of utility and helper classes.
For instance, it offers Lists.reverse() to return a reversed view of a given list.
Typically, the method accepts a list as a parameter. So, we need to call Arrays.asList() to pass an array:
@Test
public void reverseArrayUsingGuava() {
Object[] initialArray = { "A", "B", "C", "D", "E", "F" };
Object[] result = Lists.reverse(Arrays.asList(initialArray))
.toArray();
Object[] reversedArray = { "F", "E", "D", "C", "B", "A" };
assertArrayEquals(reversedArray, result);
}
As shown above, since Lists.reverse() returns a list, we used the toArray() method to get the reversed elements as an array.
Using Apache Commons Library
Another solution would be using the Apache Commons Lang library.
It provides the ArrayUtils.reverse() method for inversing an array in a null-safe manner (it accepts null as a parameter).
@Test
public void reverseArrayUsingApacheCommonsLang() {
String[] initialArray = { "A", "B", "C", "D", "E", "F" };
ArrayUtils.reverse(initialArray);
String[] reversedArray = { "F", "E", "D", "C", "B", "A" };
assertArrayEquals(reversedArray, initialArray);
}
Please bear in mind that Guava and Apache Commons are external libraries, so we need to add their dependencies in pom.xml
Conclusion
To sum it up, we have covered in-depth everything you need to know about how to reverse an array in Java.
Along the way, we have seen how to use Java 7 and Java 8 methods to achieve this.
Then, we showcased how to use external libraries such as Guava and Apache Commons to accomplish the same objective.