Summary: In this tutorial, we will learn to reverse an array in Java using Loop, Collections and methods.


There are multiple ways to reverse an array in Java. Let’s discuss some of the methods.

Reverse Array in Java using Extra Array


Input Array: 5 7 9 6 3
Reverse Array: 3 6 9 7 5

In the above program, we first create an empty reverse array of the same length as the input array.

Then we loop through the array values from the back and assigns them to the front of the reverse array.

The complexity of the above algorithm is O(n) and is not an In-Place algorithm.

Reverse Array in Java without using Extra Array

The logic to reverse an array without using another array in Java program is to swap 1st element with the last element, then the 2nd element with the 2nd last element, then 3rd, 4th… until we reach the middle element.

Reverse array without array
swap 1st half with 2nd half

Here is the implementation of the above algorithm in Java.


Input Array: 1 2 3 4 5
Reverse Array: 5 4 3 2 1

This algorithm is an In-Place algorithm because we had not used any extra array.

Although we are iterating only half of the array still the complexity of the program is equivalent to O(n) i.e. O(n/2).

Reverse Array in Java using Collections.reverse()


Input Array: 1 2 3 4 5
Reverse Array: 5 4 3 2 1

Collection.reverse() method of java.util package returns the reverse of the list passed as a parameter.

To take advantage of this inbuilt method, we convert the array into a list using Arrays.asList() method of the same package.

We then reverse the list using Collection.reverse() and then convert it back to the array using toArray() of the list class.

Since this methods includes lot of type conversions, it is not so efficient.

new Integer[0] in toArray(new Integer[0]) helps in the conversion of Object[] type to Integer[] type, which in turn is unboxed to int[] by Java.

In this Java tutorial, we learned multiple ways to reverse an array in Java with examples.

Leave a Reply