Reverse an array in Java
Java provides several ways to reverse an array using built-in methods and manual approaches. The simplest way to reverse an array in Java is by using a loop to swap elements or by using the helper methods from the Collections and Arrays classes.
Example:
Let’s take an example to reverse an array using a basic loop approach.
import java.util.Arrays;
public class ArrayReverse {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
// Swap elements from start to end
for (int i = 0; i < arr.length / 2; i++) {
int t = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = t;
}
System.out.println("" + Arrays.toString(arr));
}
}
Output
[5, 4, 3, 2, 1]
Explanation: Here, we don’t create a new array. Instead, we reverse the original array itself. In this method, we swap the elements of the array. The first element is swapped with the last element. The second element is swapped with the second-to-last element, and so on.
Table of Content
Other Ways to Reverse an Array
Using Temp array
To reverse an array using a temporary array, create a new array of the same size. Copy elements from the original array into the new array starting from the last element, filling the new array from the beginning.
// Java program to reverses an array
// using Temp array
public class ArrayReverse {
// function that reverses array and stores it
// in another array
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
// printing the reversed array
System.out.println("");
for (int k = 0; k < n; k++) {
System.out.println(b[k]);
}
}
public static void main(String[] args)
{
int [] arr = {1, 2, 3, 4, 5};
reverse(arr, arr.length);
}
}
Output
5 4 3 2 1
Note: The temporary array approach creates a new array to store reversed elements, while the basic loop approach reverses elements in the original array by swapping in place without creating a new array.
Using Collections.reverse()
The Collection.reverse() method is to use the function java.util.Collections.reverse(List list) method. This method reverses the elements in the specified list. Hence, we convert the array into a list first by using Arrays.asList() and then reverse the list. We need to use this method when working with lists for easier reversal.
// Java Program to reverse an array
// using Java collections
import java.util.*;
public class ArrayReverse {
// function reverses the elements of the array
static void reverse(Integer a[])
{
Collections.reverse(Arrays.asList(a));
System.out.println(Arrays.asList(a));
}
public static void main(String[] args)
{
Integer [] arr = {1, 2, 3, 4, 5};
reverse(arr);
}
}
Output
[5, 4, 3, 2, 1]
Using StringBuilder.append()
If we are working with a String array, we can use a StringBuilder and append each array element with a for loop decrementing from the array’s length, convert the StringBuilder to a string, and split back into an array.
// Java Program for Reversing an array
// using StringBuilder
import java.util.Arrays;
class ArrayReverse {
public static void main (String[] args) {
String[] arr = {"Hello", "World"};
StringBuilder r = new StringBuilder();
for (int i = arr.length; i > 0; i--) {
r.append(arr[i - 1]).append(" ");
};
String[] ra = r.toString().split(" ");
System.out.println(Arrays.toString(ra));
}
}
Output
[World, Hello]
Which Method to Choose for Reversing an Array in Java?
- Basic Loop (In-Place): This is used for in-place reversal when we want to modify the original array directly without using extra space and without creating new array.
- Temp Array: This method is used to create a reversed copy of the array while keeping the original array.
- Swapping: This method is ideal for in-place reversal with very few steps and no extra memory usage, swapping elements from the start and end.
- Collections.reverse(): This method is used when we need to reverse an array after converting it to a list, particularly when dealing with objects like
Integer[]
.