Java reverse array example shows how to reverse an array in Java with a temporary array, without a temporary array, using Collections, and using Apache commons library.
How to reverse an array in Java?
There are several ways in which you can reverse an array in Java as given below.
1) Reverse an array using the Collections and Arrays classes
If you want to reverse an object array, we can use the reverse
method of the Collections class and the asList
method of the Arrays class.
The asList
method of the Arrays class converts an array to a List that is backed by the original array. The reverse
method reverses a List as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.basic.arrayexamples; import java.util.Arrays; import java.util.Collections; public class JavaArrayReverseExample { public static void main(String[] args) { Integer[] arr = {1, 2, 3, 4, 5, 6}; //print original array System.out.println("Original Array"); for(int i : arr) System.out.print(i + ","); Collections.reverse(Arrays.asList(arr)); //line break System.out.println(""); //print reversed array System.out.println("Reversed Array"); for(int i : arr) System.out.print(i + ","); } } |
Output
1 2 3 4 |
Original Array 1,2,3,4,5,6, Reversed Array 6,5,4,3,2,1, |
Note: This approach does not work for an array of primitive values, for example, an array of int.
2) Using the second array
We can also use a second array and a for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
int[] intArray = {1, 2, 3, 4, 5}; int intArrayLegth = intArray.length; //create second array of the same length int[] reversedArray = new int[intArrayLegth]; //loop through original array in reverse direction for(int i= intArrayLegth - 1, j = 0; i >=0; i--, j++){ //assign elements to second array reversedArray[j] = intArray[i]; } //print original array System.out.println("Original Array"); for(int i : intArray) System.out.print(i + ","); //line break System.out.println(""); //print reversed array System.out.println("Reversed Array"); for(int i : reversedArray) System.out.print( i + ","); |
Output
1 2 3 4 |
Original Array 1,2,3,4,5, Reversed Array 5,4,3,2,1, |
3) Using a for loop and swapping elements
You can also avoid using the second array and do the in-place swapping as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
int[] intArray = {1, 2, 3, 4, 5}; int intArrayLegth = intArray.length; //print original array System.out.println("Original Array"); for(int i : intArray) System.out.print(i + ","); for(int i = 0; i < intArrayLegth/2; i++){ int tempInt = intArray[i]; intArray[i] = intArray[intArrayLegth - 1 - i]; intArray[intArrayLegth - 1 - i] = tempInt; } //line break System.out.println(""); //print reversed array System.out.println("Reversed Array"); for(int i : intArray) System.out.print(i + ","); |
Output
1 2 3 4 |
Original Array 1,2,3,4,5, Reversed Array 5,4,3,2,1, |
Basically, we are swapping the first half of the elements of an array with the second half to reverse it.
4) Using Apache Commons
If you are using the Apache Commons library, you can use the static reverse
method of the ArrayUtils class. The reverse
method has been overloaded for all the primitive types as well as for an Object type.
a) int array
1 2 3 4 5 6 7 8 |
int[] intArray = {1, 2, 3, 4, 5}; ArrayUtils.reverse(intArray); //print reversed array System.out.println("Reversed Array"); for(int i : intArray) System.out.print(i + ","); |
Output
1 2 |
Reversed Array 5,4,3,2,1, |
b) String array
1 2 3 4 5 6 7 8 |
String[] stringArray = {"One", "Two", "Three", "Four", "Five"}; ArrayUtils.reverse(stringArray); //print reversed array System.out.println("Reversed Array"); for(String str : stringArray) System.out.print(str + ","); |
Output
1 2 |
Reversed Array Five,Four,Three,Two,One, |
This example is a part of the Java Array tutorial.
Please let me know your views in the comments section below.