In this quick tutorial, we’ll learn ways in which we can invert or reverse an array. We’ll first look at the most basic Java implementations and later cover a few third-party options.
Consider we have an array of elements:
Integer[] intArr = {12, 30, 1, 7, 4};
Our problem is to be able to invert or reverse the elements of an array so that our outputArr would look like:
Integer[] outputArr = {4, 7, 1, 30, 12};
Also, we’ll try to make a generic solution that will work for all array types. So, let’s get started!
An old-school way of reversing array elements would involve swapping the elements until we iterate and reach the mid element in the array.
public Object[] reverseArray(Object[] inputArr) {
for(int i = 0; i < inputArr.length/2; i++) {
int temp = inputArr[i];
inputArr[i] = inputArr[inputArr.length - 1 - i];
inputArr[inputArr.length - 1 - i] = temp;
}
return inputArr;
}
Here, we have inverted the array with no extra space except for a temporary variable. Also, it’s pretty efficient as we are iterating only until we reach the mid element in the array.
We can also invert our array using Java 8 Streams API:
public Object[] reverseArray(Object[] inputArr) {
return IntStream.rangeClosed(1, inputArr.length)
.mapToObj(i -> inputArr[inputArr.length - i])
.toArray();
}
This approach is not pretty intuitive. Here, we generated a stream of integers and then mapped them as array indexes of the elements in the reverse order.
Collections class has a reverse() method which can reverse elements of say a List or any other collection. We can use it to our advantage by first converting our array into a List. Once we have reversed its elements, we can again convert that reversed list into an array.
public Object[] reverseArray(Object[] inputArr) {
List<Object> list = Arrays.asList(inputArr);
Collections.reverse(list);
return list.toArray();
}
This code looks more readable and intuitive than the one using Streams API.
ArrayUtils class in the Apache Commons Lang library provides a reverse() method which can serve our purpose.
ArrayUtils.reverse(array);
This is a pretty cool and quick way to reverse our array. All we need to make sure is to have the dependency of the library in our POM:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
The latest version is easily available at Maven Central.
Google Guava’s library has a method to reverse a List which is another option that we can opt for. Our code would look similar to:
public Object[] reverseArray(Object[] inputArr) {
List<Object> list = Arrays.asList(inputArr);
List<Object> reversedList = Lists.reverse(inputArr);
return reversedList.toArray();
}
Also, we must make sure that we include the library dependency in our POM:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
The latest version can be looked out at Maven Central.
In this short tutorial, we learned to solve the basic problem of reversing an array. I hope that helps!
There’s an article Java Code Geeks-Arrays that I recommend checking out to know about Java arrays in general.