Open In App

Reverse an array in Java

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.

Approaches

There are numerous approaches to reverse an array in Java. These are:



1. Using Temp array

The first method is as follows: 




// 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);
    }
}

Output
Reversed array is: 

50
40
30
20
10

2. Using Swapping

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 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);
    }
}

Output
Reversed array is: 

50
40
30
20
10

3. Using Collections.reverse() method

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. 
 




// Reversing an array using Java collections
import 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);
    }
}

Output
[50, 40, 30, 20, 10]

4. Using StringBuilder.append() method

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 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));
    }
}

Output
[World, Hello]

Article Tags :