Reverse an array in Java - GeeksforGeeks

08 Apr, 2022
Given an array, the task is to reverse the given array in Java.
Examples: 
Input : 1, 2, 3, 4, 5
Output :5, 4, 3, 2, 1

Input :  10, 20, 30, 40
Output : 40, 30, 20, 10
To know about the basics of Array, refer to Array Data Structure.
There are numerous approaches to reverse an array in Java. These are:
Using Temp array
Using Swapping
Using Collections.reverse() method
Using StringBuilder.append() method
The first method is as follows: 
Take input the size of the array and the elements of the array.
Consider a function reverse which takes the parameters-the array(say arr) and the size of the array(say n).
Inside the function, a new array (with the array size of the first array, arr) is initialized. The array arr[] is iterated from the first element, and each element of array arr[] is placed in the new array from the back, i.e., the new array is iterated from its last element.
In this way, all the elements of the array arr[] are placed reversely in the new array.
Further, we can iterate through the new array from the beginning and print the elements of the array.
Java
// Basic Java program that reverses an array  public class reverseArray {      // 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("Reversed array is: \n");        for (int k = 0; k < n; k++) {            System.out.println(b[k]);        }    }      public static void main(String[] args)    {        int [] arr = {10, 20, 30, 40, 50};        reverse(arr, arr.length);    }}
Reversed array is: 

50
40
30
20
10
The second method uses a similar code for the inputting and printing of the array. However, we don’t create a new array like the above method. 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 last but one element and so on. For instance, consider array [1, 2, 3, ...., n-2, n-1, n]. We swap 1 with n, 2 with n-1, 3 with n-2 and further.  
Java
// Java Program that reverses array// in less number of swaps  public class arrayReverse {      // function swaps the array's first element with last    // element, second element with last second element and    // so on    static void reverse(int a[], int n)    {        int i, k, t;        for (i = 0; i < n / 2; i++) {            t = a[i];            a[i] = a[n - i - 1];            a[n - i - 1] = t;        }          // printing the reversed array        System.out.println("Reversed array is: \n");        for (k = 0; k < n; k++) {            System.out.println(a[k]);        }    }      public static void main(String[] args)    {        int[] arr = { 10, 20, 30, 40, 50 };        reverse(arr, arr.length);    }}
Reversed array is: 

50
40
30
20
10
The third 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 java.util.Arrays.asList(array) and then reverse the list.  
Java
// Reversing an array using Java collectionsimport java.util.*;  public class reversingArray {      // 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 = {10, 20, 30, 40, 50};        reverse(arr);    }}
[50, 40, 30, 20, 10]
As a fourth method, If you 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
// Java Program for Reversing an array using StringBuilder  import java.util.Arrays;  class GFG {    public static void main (String[] args) {      String[] arr = {"Hello", "World"};      StringBuilder reversed = new StringBuilder();        for (int i = arr.length; i > 0; i--) {          reversed.append(arr[i - 1]).append(" ");      };              String[] reversedArray = reversed.toString().split(" ");              System.out.println(Arrays.toString(reversedArray));    }}
[World, Hello]
briangaedu
nishkarshgandhi
Java-Array-Programs
Java-Arrays
Picked
Technical Scripter 2018
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java