JavaDevNotes.com

Java Reverse Array

Developers loves to use arrays to store multiple values whenever possible because of it's simplicity and ease of use. It is not very flexible as the other classes in the collections API, but all codes for it are very simple and easy to understand. Once we have stored multiple values, we usually want the reverse order for some other processing. For example, if an array is listed in ascending order, we may want to reverse it to get the descending order. Below are some examples of how we could reverse an array in Java.

The Problem

Given an array, for example a list of integers like below:
int[] intArray = {100, 125, 500, 300, 900};
We want to reverse it to get an equivalent of the array below:
int[] intArray = {900, 300, 500, 125, 100};

We should be able to use the algorithm to any array of objects, for example, if we want to reverse this String array:

String[] stringArray = {"Cheese", "Potato", "Apple"};

We want to reverse it in such a way that we get:

String[] stringArray = {"Apple", "Potato", "Cheese"};

Reverse Array Using For Loop

The simplest way that we could solve the problem of reversing an array in Java is by using our own algorithm with for loop statement. For loop is essential because we don't know how many items the array have. We will first get the number of items in the array, or the length of the array, and then use that information to loop. Below is a wrong answer of reversing the array. Could you spot why?
int arrayLength = intArray.length; 
for (int i = 0; i < arrayLength-1; i++) {
        int temp = intArray[i];
        intArray[i] = intArray[arrayLength - 1 - i];
        intArray[arrayLength - 1 - i] = temp;
}
What we did is we swap the first and last items first. And then we swap the second and second to the last items. And we do this until we visited all the items. The problem with this approach is that once we go past the mid point of the array, we also start returning the array to it's original order. And so the end result of this operation is we go back to the original array and we have failed to reverse it. The correct answer is just to do the loop for upto the mid point only. Here is the revised solution that works:
int arrayLength = intArray.length; 
for (int i = 0; i < arrayLength/2; i++) {
        int temp = intArray[i];
        intArray[i] = intArray[arrayLength - 1 - i];
        intArray[arrayLength - 1 - i] = temp;
}
The simple solution is just to use arraylengt/2 as the number of loops we perform the swapping logic. It is just that simple but effective. Now it is reversing the array.

Reverse Array Using Collections.reverse()

The Collections utility class has many nice methods to help us with common task. One of this is reversing a List. But since we have an array, we can't use Collections.reverse(). We must first convert our array to list, invoke Collections.reverse(), and then convert it back to array. Here is the code to make it clearer:
String[] stringArray = {"Chicken", "Soup", "Soul"};
List<String> list = Arrays.asList(stringArray);
Collections.reverse(list);
list.toArray(stringArray);
The method Arrays.asList converts our array into a list. And then after calling the Collections utility class, we invoke the toArray method to transfer back the value of the list to our array. Now the array is reversed. Pretty complicated and slow process for a simple thing we wish to perform.

Reverse Array Using Java 8 Stream API

Java8 introduces the Stream API. This is my least favorite as the code using Stream API does not look very intuitive for me. But in case it is of interest, below is a solution on reversing an array of Integer using Java 8 Stream API:
Integer[] intArray = {500, 200, 300, 1000, 450};
Object[] reverseArray = IntStream.rangeClosed(1, intArray.length)
    .mapToObj(i -> intArray[intArray.length - i])
    .toArray();
And the result is the same, with the caveat that the return type if Array of Objects, where each item is of type Integer.