Skip to content
Related Articles

Related Articles

Reverse an array in Java
  • Difficulty Level : Easy
  • Last Updated : 11 Dec, 2018

Reversing an array in java can be done in three simple methods.

Examples:

Input : 1, 2, 3, 4, 5
Output :5, 4, 3, 2, 1

Input :  10, 20, 30, 40
Output : 40, 30, 20, 10

The first method is as follows:
(i) Take input the size of array and the elements of array.
(ii) Consider a function reverse which takes the parameters-the array(say arr) and the size of the array(say n).
(iii) 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.
(iv) In this way, all the elements of the array arr[] are placed reversely in the new array.
(v) Further, we can iterate through the new array from the beginning and print the elements of the array.




/* 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

The second method uses the 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 id 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.






/* 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

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]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :