Reverse An Array In Java – 3 Methods With Examples

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 7, 2024

Reversing an Array is one of the Crucial Operations in Java. In this tutorial, we will Learn how to Reverse an Array in Java:

Sometimes programmers need to process arrays starting with the last element, in that case, it is always efficient to reverse the array so that the first element is placed at the last position in the array, and the second element is placed at the second last position in the array and so on until the last element is at the first index.

=> Check Out The Ultimate Java Training Series Here

Reverse an Array in Java

Let’s consider an array as shown below:

Array

After applying the reverse functionality, the resultant array should be like:

Array after reverse functionality

Printing Array In Reverse Order

Alternatively, if we want to print the array in the reverse order, without actually reversing it, then we can do that just by providing a for loop that will start printing from the end of the array. This is a good option as long as we just want to print the array in reverse order without doing any processing with it.

The following program prints the array in reverse order.

import java.util.*;
import java.util.stream.*;
public class Main
{
	public static void main(String[] args) {
	Integer[] intArray = {10,20,30,40,50,60,70,80,90};
	
  //print array starting from first element
	System.out.println("Original Array:");
	for(int i=0;i<intArray.length;i++)
	     System.out.print(intArray[i] + "  ");
	
	System.out.println();
	
	//print array starting from last element
	System.out.println("Original Array printed in reverse order:");
	     for(int i=intArray.length-1;i>=0;i--)
	     System.out.print(intArray[i] + "  ");
	}
}

Output:

Printing array in Reverse Order

This is a feasible option to print the array only.

Java provides various methods to actually reverse the indices of elements in the array. Enlisted below are the various methods that we are going to discuss in detail in this tutorial.

  • Using ArrayList reverse method
  • Using traditional for loop
  • Using in-place reversal

Reverse An Array Using ArrayList

Reversing an array in Java can be done using the ‘reverse’ method present in the collections framework. But for this, you first need to convert an array to a list as the ‘reverse’ method takes the list as an argument.

The following program reverses an array using the ‘reverse’ method.

import java.util.*; 

public class Main { 

    /*function reverses the elements of the array*/
    static void reverse(Integer myArray[]) 
    { 
        Collections.reverse(Arrays.asList(myArray)); 
        System.out.println("Reversed Array:" + Arrays.asList(myArray)); 
    } 

     public static void main(String[] args) 
    { 
        Integer [] myArray = {1,3,5,7,9}; 
        System.out.println("Original Array:" + Arrays.asList(myArray));
        reverse(myArray); 
    } 
}

Output:

Reverse an array using ArrayList

In this program, we use the reverse function on an array by changing it into the list.

In a similar manner, we can also reverse a string array as shown in the following example.

Example:

import java.util.*;

public class Main {

    /*function reverses the elements of the array*/
    static void reverse(String myArray[])
    {
        Collections.reverse(Arrays.asList(myArray));
        System.out.println("Reversed Array:" + Arrays.asList(myArray));
    }

     public static void main(String[] args)
    {
        String [] myArray = {"one", "Two", "Three", "Four", "Five", "Six","Seven"};
        System.out.println("Original Array:" + Arrays.asList(myArray));
        reverse(myArray);
    }
}

Output:

String array

The above program defines a string array. By converting it to the list and using the reverse method on it, we reverse the array.

Reverse An Array Using Traditional For Loop

Yet another approach for reversing the array is to write a separate method to reverse an array in which you can have a new array and put the elements of the original array into this new array in a reverse manner.

Check the following implementation.

public class Main { 

     static void reverse_array(char char_array[], int n) 
    { 
       char[] dest_array = new char[n]; 
       int j = n; 
       for (int i = 0; i < n; i++) { 
            dest_array[j - 1] = char_array[i]; 
            j = j - 1; 
        } 

        System.out.println("Reversed array: "); 
        for (int k = 0; k < n; k++) { 
           System.out.print(dest_array[k] + " "); 
        } 
    } 

    public static void main(String[] args) 
    { 
        char [] char_array = {'H','E','L','L','O'}; 
           System.out.println("Original array: "); 
        for (int k = 0; k <char_array.length; k++) { 
             System.out.print(char_array[k] + " "); 
        }
    System.out.println();
    reverse_array(char_array, char_array.length); 
    } 
}

Output:

Reverse an array using traditional for loop

Here we have used a character array as an example. Using the reverse function, we reverse the array elements one by one and then display the reversed array.

In-place Reversal Of Array

The third method of array reversal is reversing the elements of array in-place without using a separate array. In this method, the first element of the array is swapped with the last element of the array. Similarly, the second element of the array is swapped with the second last element of the array and so on.

This way at the end of array traversal, we will have the entire array reversed.

The following program demonstrates in-place reversal of array.

import java.util.Arrays;
public class Main { 

    /*swap the first elemnt of array with the last element; second element with second last and so on*/
    static void reverseArray(intintArray[], int size) 
    { 
        int i, k, temp; 
        for (i = 0; i < size / 2; i++) { 
            temp = intArray[i]; 
            intArray[i] = intArray[size - i - 1]; 
            intArray[size - i - 1] = temp; 
        } 

        /*print the reversed array*/
       System.out.println("Reversed Array: \n" + Arrays.toString(intArray)); 
    } 

    public static void main(String[] args) 
    { 
         int [] intArray = {11,22,33,44,55,66,77,88,99};

        //print the original array
        System.out.println("Original Array: \n" + Arrays.toString(intArray)); 
        //function call to reverse the array
        reverseArray(intArray, intArray.length); 
    } 
}

Output:

In-place reversal of array

As shown in the output, the program generates a reversed array by swapping the elements in the original array itself without using the second array. This technique is more efficient as it saves memory space.

Frequently Asked Questions

Q #1) How do you Reverse an Array in Java?

Answer: There are three methods to reverse an array in Java.

  • Using a for loop to traverse the array and copy the elements in another array in reverse order.
  • Using in-place reversal in which the elements are swapped to place them in reverse order.
  • Using the reverse method of the Collections interface that works on lists.

Q #2) How do you Reverse a List in Java?

Answer: You can use the reverse method provided by the Collections interface of Java.

Q #3) Which method of Reversing an Array is better?

Answer: Normally, converting an array to list and reversing it using the reverse method is best. Also, in-place reversal is better than using another array to reverse the array as this saves on memory.

Conclusion

In this tutorial, we discussed the various methods to reverse an array in Java. Though for demonstration purposes we have used integer data, you can apply the same methods to reverse the array with any other data whether primitives or non-primitives.

In our subsequent tutorials, we discuss more topics on arrays like exceptions, string arrays, etc.

=> Read Through The Simple Java Guide Here

Was this helpful?

Thanks for your feedback!

Leave a Comment