Reverse an Array in Java
Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.
Let us first see the most common way to reverse an array in Java, then we will discuss other ways.
Example: Reverse Using a Loop
This is the most common way to reverse an array. We just have to swap the first element with the last, the second with the second last, and so on.
// Java Program to reverse an array
// using loop
import java.util.Arrays;
public class Geeks {
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: We use this loop approach because it does not need any extra space. Also, it updates the original array.
Other Ways to Reverse an Array
Using a Temporary Array
If we want to keep our original array as it is, then we can copy the values in reverse order into a new array.
Example:
// Java program to reverses an array
// using Temp array
public class Geeks {
// 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.
Explanation: This approach is useful when we don't want to change the original array.
Using Collections.reverse()
This method is for arrays of objects like Integer[]. We can convert the array into a list by using Arrays.asList() and then use Collection.reverse() to reverse it easily.
// Java Program to reverse an array
// using Java collections
import java.util.*;
public class Geeks {
// 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]
Explanation: This method is useful when working with lists or object arrays.
Using StringBuilder.append()
When 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, then 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 Geeks {
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]
When to Use Which Approach?
Methods | Best For |
---|---|
Loop | When we want to reverse the array without extra space. |
Temporary Array | When we need a new reversed copy and keep the original array. |
Collections.reverse() | When we are working with object arrays like Integer[]. |
StringBuilder | When reversing a string array in a creative way. |