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
// 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));
}
}
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
// 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);
}
}
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
// 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);
}
}
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
// 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));
}
}
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. |
---|
Similar Reads
Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3
3 min read
Perl | Reverse an array Reverse an array or string in Perl. Iterative Way: Iterate over the array from 0 to mid of array. Swap the arr[i] element with arr[size-i] element. Perl #Perl code to reverse an array iteratively #declaring an array of integers @arr = (2, 3, 4, 5, 6, 7); # Store length on array in $n variable $n = $
2 min read
How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of
5 min read
Reverse an ArrayList in Java using ListIterator Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an ar
6 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
System.arraycopy() in Java java.lang.System class provides useful methods for standard input and output, for loading files and libraries or to access externally defined properties. The java.lang.System.arraycopy() method copies a source array from a specific beginning position to the destination array from the mentioned posit
2 min read
Reverse a LinkedList in Java Assuming you have gone through LinkedList in java and know about linked list. This post contains different examples for reversing a linked list which are given below: 1. By writing our own function(Using additional space): reverseLinkedList() method contains logic for reversing string objects in a l
7 min read
Integer reverse() Method In Java The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax: public static int reverse(int a) Parameters: The parameter a is an integer value whose bits are to be rev
2 min read
Reverse an array using Stack Given an array arr[] of size n, the task is to reverse the array using Stack.Examples:Input: arr = [10, 20, 30, 40, 50] Output: 50 40 30 20 10 Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.Input: arr = [ 1 ]Output: 1Explanation: Reversing
4 min read
JavaScript Array toReversed() Method The toReversed() method in Javascript arrays returns a new array with the elements in reversed order and the original array remains unaffected. The toReversed() method, introduced in ECMAScript 2023, offers a way to reverse the order of elements in an array. Unlike the sort() method, it creates a ne
1 min read