Introduction

In this tutorial, we will discuss how we can reverse an array in Java. You are given an array, the task is to reverse the array using Java language.

Example

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

Input: 5, 7, 1, 3, 5
Output: 5, 3, 1, 7, 5

Reverse an Array Methods

There are numerous ways to reverse an array and they are the following:

Additional array method

In this method, we declare an auxiliary array of the same size as the original array. We will then start iterating over the original array in reverse order and starting to fill the additional array from the beginning.

Let us consider the following program that reverses an array:

Source Code:

import java.util.*;

class HelloWorld {


// function that reverses array and stores it
    // in another array
    static void reverseTheArray(int arr[], int n)
    {
       
        // Declare an auxiliary array
        int[] auxiliary = new int[n];
       
        int j = n;
        for (int i = 0; i < n; i++) {
           
            // Fill the auxiliary array
            auxiliary[j - 1] = arr[i];
            j = j - 1;
        }
 
        // printing the reversed array
        System.out.println("The array is:");
        for (int i = 0; i < n; i++) {
            System.out.println(auxiliary[i]);
        }
    }

    public static void main(String[] args) {
       
        // Initialize an array
        int [] arr = {10, 20, 30, 40, 50};
        reverseTheArray(arr, arr.length);
       
    }
}

Output:

write your code here: Coding Playground

Swap method

In this method, we initialize two variables left and right as 0 and N - 1 respectively and start swapping elements by incrementing and decrementing the variables by one respectively.

Let us consider the following program that reverses an array:

Source Code:

class HelloWorld {
    public static void main(String[] args) {
       
        // Initialize an array
        int[] arr = {10, 20, 30, 40, 50};
       
        // Initialize two variable
        // to traverse the array
        int left = 0, right = 4;
       
        // Iterate till left is
        // lesser than right
        while(left < right)
        {
           
            // Swap values
            int third = arr[left];
            arr[left] = arr[right];
            arr[right] = third;
            left++;
            right--;
        }
       
        // Print array elements
        System.out.println("The array elements are:");
        for(int index = 0 ; index < 5 ; index++)
        {
            System.out.println(arr[index]);
        }
    }
}

Output:

Output Description:

As you can see in the output the array has been reversed.

write your code here: Coding Playground

Using collections.reverse() method

Java provides us collections.reverse() method using which we can reverse an array easily. This function has the following syntax:

Syntax:

Collections.reverse(Arrays.asList(array))

Source Code:

import java.util.*;

class HelloWorld {
   
    public static void reverseTheArray(Integer arr[])
    {
        // Reverse the array using collections
        Collections.reverse(Arrays.asList(arr));
        System.out.println("The array elements are:");
       
        // Print the array
        System.out.println(Arrays.asList(arr));
    }
   
    public static void main(String[] args) {
       
        // Initialize an integer array
        Integer [] arr = {10, 20, 30, 40, 50};
       
        // Reverse the array
        reverseTheArray(arr);
    }
}

Output:

write your code here: Coding Playground

Output Description:

As you can see in the output the array has been reversed.

Using stringBuilder.append() method

Source Code:

import java.util.*;

class HelloWorld {

   
    public static void main(String[] args) {
       
      // Initialize a string vector
      String[] arr = { "Infinity", "Board"};
       
      // Create an stringBuilder object
      StringBuilder object = new StringBuilder();
 
      // Append into stringBuilder object
      for (int i = arr.length; i > 0; i--) {
          object.append(arr[i - 1]).append(" ");
      };
     
      // Get the reversed array
      String[] reversedArray = object.toString().split(" ");
     
      System.out.println("The array elements are:");
     
      // Print the reversed array
      System.out.println(Arrays.toString(reversedArray));
       
    }
}

Output:

write your code here: Coding Playground

Conclusion

In this tutorial, we discussed different ways to reverse an array in Java. We believe this tutorial has surely helped you to enhance your knowledge in your field of you.