Print contents of an array in reverse order in Java
This post will discuss how to print the contents of an array in reverse order in Java.
1. Print array in reverse order
A simple solution to print the array contents in reverse order is using a simple for loop. The loop starts at the end of the array, prints the current element, and decrement the index by 1 at every iteration.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for (int i = arr.length - 1; i >= 0 ; i--) { System.out.println(arr[i]); } } } |
Output:
5
4
3
2
1
Here’s a version using Java 8 streams:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; IntStream.iterate(arr.length - 1, i -> i >= 0, i -> i - 1) .map(i -> arr[i]) .forEach(System.out::println); } } |
Output:
5
4
3
2
1
2. In-place Reverse Array
If you need to reverse an array in-place, you can use any of the following methods:
a. Using For loop
The idea is to swap items starting from both ends of the array until they cross each other. This can be easily done using a for loop:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; public class Main { public static void reverse(int[] arr) { if (arr == null) { return; } for (int i = 0, j = arr.length - 1; j > i; i++, j--) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; reverse(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
The above solution uses two indices, one for each end of the array. The code can be easily modified to use only a single index:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; public class Main { public static void reverse(int[] arr) { if (arr == null) { return; } for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = temp; } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; reverse(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
b. Using Apache Commons Lang Library
Another good alternative to reverse the order of an array is using Apache Commons Lang ArrayUtils.add() method, which is overloaded for all primitives types and object arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; ArrayUtils.reverse(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
c. Using Guava
With Guava, you can call the Ints.asList() method to get a fixed-size list backed by the specified array. Then call the Collections.reverse() method to reverse the order of the elements in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; Collections.reverse(Ints.asList(arr)); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
3. Create a Reversed Copy
If you don’t need to modify the original array, the following solution creates a reversed copy of it. The idea is to create a new array of the same size as the original array. Then fill the new arrays with the elements of the original array in reverse order using a loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] reverse = new int[arr.length]; int count = 0; for (int i = 1; i <= arr.length; i++) { reverse[count++] = arr[arr.length - i]; } System.out.println(Arrays.toString(reverse)); } } |
Output:
[5, 4, 3, 2, 1]
Here’s a version using Java 8 streams:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] reverse = IntStream.rangeClosed(1, arr.length) .map(i -> arr[arr.length-i]) .toArray(); System.out.println(Arrays.toString(reverse)); } } |
Output:
[5, 4, 3, 2, 1]
That’s all about printing contents of an array in reverse order in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)