Reverse an array in Java
This post will discuss how to reverse an array in Java.
1. Naive solution
A simple solution is to create a new array of the same type and size as the input array, fill it with elements from the original array in reverse order, and then copy the contents of the new array into the original one.
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 |
import java.util.Arrays; class Main { // Utility method to reverse elements of an array public static void reverse(int[] nums) { int[] temp = new int[nums.length]; for (int i = 0; i < nums.length; i++) { temp[nums.length - 1 - i] = nums[i]; } for (int i = 0; i < nums.length; i++) { nums[i] = temp[i]; } } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; reverse(nums); System.out.println(Arrays.toString(nums)); } } |
2. In-place Implementation
The above implementation requires O(n) extra space for the auxiliary array. We can avoid that by modifying the array in-place. That means that the conversion should take place without using an auxiliary array, by overwriting the existing array elements.
An in-place algorithm can be implemented by reading the elements from both ends of the array and swap them, as shown 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 |
import java.util.Arrays; class Main { // Utility method to swap two elements nums[i] and nums[j] in the array private static void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } // Utility method to reverse elements of an array public static void reverse(int[] nums) { for (int low = 0, high = nums.length - 1; low < high; low++, high--) { swap(nums, low, high); } } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; reverse(nums); System.out.println(Arrays.toString(nums)); } } |
We can also use recursion for reversing an array without using an auxiliary array. The logic remains the same as the above iterative implementation, but it takes space for the call stack.
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 29 30 31 32 33 34 |
import java.util.Arrays; class Main { // Utility method to swap two elements nums[i] and nums[j] in the array private static void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } // Recursive method to reverse elements of a subarray formed by nums[low, high] public static void reverse(int[] nums, int low, int high) { if (low < high) { swap(nums, low, high); reverse(nums, low + 1, high - 1); } } // Utility method to reverse elements of an array public static void reverse(int[] nums) { reverse(nums, 0, nums.length-1); } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; reverse(nums); System.out.println(Arrays.toString(nums)); } } |
3. Using Stack (Implicit and Explicit stack)
Another plausible way of reversing an array is to use the stack data structure. The idea is to push each array element into a stack. Then we simply pop values from the stack one by one and assign each popped item back to the original array starting from the beginning.
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 29 30 31 32 33 34 |
import java.util.Arrays; import java.util.Stack; class Main { // Utility method to reverse elements of an array public static void reverse(int[] nums) { // create an empty stack of integers Stack<Integer> stack = new Stack<>(); // push each array element into a stack for (int i: nums) { stack.push(i); } // start from index 0 int index = 0; // pop values from the stack until it is empty while (!stack.isEmpty()) { // assign each popped item back to the original array nums[index++] = stack.pop(); } } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; reverse(nums); System.out.println(Arrays.toString(nums)); } } |
We can easily convert the above code to use recursion the call stack instead of the explicit stack. Here’s a recursive program that demonstrates it:
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 29 30 31 |
import java.util.Arrays; class Main { // Utility method to reverse elements of an array public static void reverse(int[] nums, int nextIndex) { // base case: empty array or end of the array is reached if (nums == null || nextIndex == nums.length) { return; } // store next element of the array int value = nums[nextIndex]; // reach the end of the array using recursion reverse(nums, nextIndex + 1); // put elements in the call stack back into an array // starting from the beginning nums[nums.length - nextIndex - 1] = value; } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; reverse(nums, 0); System.out.println(Arrays.toString(nums)); } } |
4. Using Collections.reverse()
method
Finally, we can use the reverse()
method of the Collections
class to reverse the order of the elements in the specified array. Since it accepts a collection as an argument, the idea is to pass a list wrapper backed by the given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.Collections; class Main { public static void main(String[] args) { Integer[] nums = {1, 2, 3, 4, 5}; Collections.reverse(Arrays.asList(nums)); System.out.println(Arrays.toString(nums)); } } |
That’s all about reversing an array 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 :)