Java – Array Reverse
To reverse Array in Java, use looping statement to traverse through the array and reverse the array, or use ArrayUtils.reverse() method of Apache’s commons.lang package.
If you are using commons.lang library in your application, you can directly use ArrayUtils class to reverse an Array. If you cannot use third party packages in your application, then you can reverse an array by traversing through the array using a looping statement and reverse arrange the elements of array.
In this tutorial we will learn to reverse an Array using different procedures with examples.
Example 1 – Inplace Array Reverse – ArrayUtils.reverse()
In this example, we will take an integer array and use ArrayUtils.reverse() to reverse the array.
ArrayUtils.reverse() reverses the array inplace. In other words, the method modifies the original method.
Java Program
import org.apache.commons.lang.ArrayUtils;
/**
* Java Program - Reverse Array
*/
public class ArrayReverse {
public static void main(String[] args) {
//two arrays
int[] arr1 = {1, 4, 9};
//inplace reverse
ArrayUtils.reverse(arr1);
//print the array
for(int num: arr1)
System.out.println(num);
}
}
Console Output
9
4
1
If you would like to keep the original array, you may first clone the array into another variable, and then reverse the array.
In the following example program, we cloned arr1
to result
. And after that, we applied the reverse function on the result
array.
Java Program
import org.apache.commons.lang.ArrayUtils;
/**
* Java Example Program, to Reverse Array
*/
public class ArrayReverse {
public static void main(String[] args) {
//two arrays
int[] arr1 = {1, 4, 9};
//array reverse
int[] result = arr1.clone();
ArrayUtils.reverse(result);
//print the array
for(int num: result) System.out.println(num);
}
}
Output
9
4
1
The integer array is reversed.
Example 2 – Reverse Python Array – For Loop
In this example, we shall use Java For Loop to reverse the array. Following is the sequence of steps, that we shall execute in the below program.
- Create an empty
result
array with the same size as that of original array. - Use for loop, to traverse through the elements of the original array
arr1
from start to end. While traversing, store the elements in theresult
array from end to start. - After the loop is over,
result
array contains the elements of original arrayarr1
arranged in a reverse order.
Java Program
/**
* Java Program - Reverse Array
*/
public class ArrayReverse {
public static void main(String[] args) {
int[] arr = {1, 4, 9};
//array reverse
int[] result = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
result[arr.length-1-i] = arr[i];
}
//print the array
for(int num: result)
System.out.println(num);
}
}
Output
9
4
1
Example 3 – Reverse Array using While Loop
We can also use Java While Loop statement to reverse an array.
In the following example, we have take an array of integers and reverse this array using while loop.
Java Program
/**
* Java Program - Reverse Array
*/
public class ReverseArray {
public static void main(String[] args) {
//number
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//variable to hold reversed array
int[] reverse = new int[nums.length];
//reverse array
int i = 0;
while( i < nums.length ) {
reverse[nums.length-1-i] = nums[i];
i++;
}
//print reversed array
for(int n: reverse)
System.out.print(n+" ");
}
}
Output
9 8 7 6 5 4 3 2 1
Conclusion
In this Java Tutorial, we learned how to reverse an array using different ways like ArrayUtils.reverse() , for loop and array indexing, etc.