Summary: In this tutorial, we will learn to reverse an array in Java using Loop, Collections and methods.
Example:
Array : [5, 7, 9, 6, 3]
Reverse : [3, 6, 9, 7, 5]
There are multiple ways to reverse an array in Java. Let’s discuss some of the methods.
Reverse Array in Java using Extra Array
class Main {
public static void main(String[] args) {
int array[] = {5, 7, 9, 6, 3};
//Create new array of same size
int reverse[] = new int[array.length];
int k=0;
for(int i=array.length-1; i>=0; i--){
reverse[i] = array[k++];
}
System.out.print("Input Array: ");
display(array);
System.out.print("Reverse Array: ");
display(reverse);
}
//Function to display array
private static void display(int array[]){
for(int i=0; i<array.length; i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
}
Output
Input Array: 5 7 9 6 3 Reverse Array: 3 6 9 7 5In 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.
Here is the implementation of the above algorithm in Java.
class Main {
public static void main(String[] args) {
int array[] = {1, 2, 3, 4, 5};
int temp, size = array.length;
//Display input array
System.out.print("Input Array: ");
display(array);
for(int i=0; i<size/2; i++){
temp = array[size-1-i];
array[size-1-i] = array[i];
array[i] = temp;
}
//Display reverse array
System.out.print("Reverse Array: ");
display(array);
}
//Function to display array
private static void display(int array[]){
for(int i=0; i<array.length; i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
}
Output
Input Array: 1 2 3 4 5 Reverse Array: 5 4 3 2 1This 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()
import java.util.*;
class Main {
public static void main(String[] args) {
Integer array[] = {1, 2, 3, 4, 5};
//Display input array
System.out.println("Input Array: "+Arrays.toString(array));
List<Integer> array_list = Arrays.asList(array);
Collections.reverse(array_list);
array = array_list.toArray(new Integer[0]);
//Display reverse array
System.out.println("Reverse Array: "+Arrays.toString(array));
}
}
Output
Input Array: 1 2 3 4 5 Reverse Array: 5 4 3 2 1Collection.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.