Reverse an array in Java explained with examples

Read Time:5 Minute, 41 Second

Table of Contents

When programmers process arrays starting with the last element, it is always more efficient to reverse the array so that the first element is placed at the array’s last position. Then, the second element is at the array’s second last position until the last element is at the first index.

One of Java’s Crucial Operations is reversing an array. It is a common array-based coding challenge that programmers frequently pose during the first few rounds of interviews to determine whether or not they can code. You can solve this problem in several ways, and we’ll look at the most frequent ones in Java. Because it isn’t focused on data types, this approach works with any sort of array, including string and integer arrays and object arrays.

The first approach for reversing an array is to do so in a brute force way, without requiring additional data structures or library methods. Yes, you can also reverse the array by developing your function that goes through the array and swaps elements until the array is sorted. That is the best way to approach coding interviews.

Reverse an array in Java

In Java, there are several ways to reverse an array. These are the following:

  • Using Collections.reverse() method
  • Reverse an Array Using a For Loop
  • Using StringBuilder.append() method
  • In-place array reversal
  • Using Swapping
  • Using ArrayUtils.reverse()

Printing an Array in Backwards Order

If we wish to print the array in reverse order without reversing it, we can do so by using a for loop that starts writing from the end of the array. It is an excellent solution if we only want to print the array in reverse order without processing.


The array is printed in reverse order in the following software.

It is a viable alternative for printing the array simply. Java has several methods for reversing the indices of elements in an array. The many ways that we will go through in-depth in this tutorial are given below.

  • Making use of the ArrayList reverse method
  • Looping with the usual method
  • Using reversal in-place

Using Collections.reverse() method: ArrayList is used to reverse an array

The ‘reverse’ method in the collections framework is used to reverse an array in Java.

However, because the ‘reverse’ method accepts a list as an argument, you must first convert an array to a list. The ‘reverse’ technique is used in the following application to reverse an array.

We utilize the reverse function in this program on an array to convert it to a list. Further, we may reverse a string array similarly, as illustrated in the following example.


The software above creates a string array. We reverse the array by changing it to a list and invoking the reverse function on it.

How to reverse an array using a For Loop

Another method of reversing an array is to create a new array and reverse the elements of the previous array. Take a look at the following example.

As an example, we’ve utilized a character array. We reverse the array items one by one using the reverse function and then display the reversed array.

Employing the StringBuilder.append() method

If you’re working with a String array, you may use a StringBuilder to attach each array element with a for loop decrementing from the array’s length, convert the StringBuilder to a string, and divide it back into an array as a fourth method.

In-place array reversal

Reversing the elements of an array in-place without utilizing a separate array is the third way of array reversal. The array’s initial component is swapped with the last element of the array in this function. Similarly, the array’s second element is exchanged with the array’s second last element, and so on. We’ll have the full array inverted at the end of the array traversal. In-place array reversal is demonstrated in the following program.


The software generates a reversed array by swapping the elements in the original array without using the second array, as seen in the output. This method is more efficient because it uses less memory.

Putting Swapping to Work

The array is input and printed in the second procedure using a similar technique. However, unlike the previous way, we do not build a new array. Instead, we invert the array’s original order.

The array’s elements are swapped in this procedure. The first and last elements are switched around. The second element replaces the last but one element, and so on. Take, for example, the array [1, 2, 3,…., n-2, n-1, n]. We change 1 to n, 2 to n-1, 3 to n-2, and so on.

Using ArrayUtils.reverse()

Apache Commons is an open-source library containing many utility libraries required for Java software development. This library should be included by default in Java projects to complement JDK. Apache commons-lang provides an ArrayUtils class with overloaded reverse() methods to reverse int, float, or object arrays in Java. This method also reverses the given array in place rather than returning a new one.

As you can see, we’ve successfully reversed the array with just one line. You must include commons-lang3-3.4.jar in your application’s classpath to use the ArrayUtils class from Apache commons-lang. You can use Maven instead by having the following dependency in your pom.xml file.


Example 1: Program for array reversal using Java Collections

Example 2 : Program that reverses an array


Arrays are a common coding technique for storing several values of the same kind in a single variable. There are various reasons why a programmer might want to reverse an array. When the logic of an issue requires starting from the last entry, it may be necessary to reverse an array.

This article looked at how to reverse an array in Java using several techniques. Though we utilized integer data for demonstration purposes, the same methods are used to reverse the array with any other data, whether primitives or non-primitives.

For specific routines in Java, the most primitive types of arrays — int, long, string, and double – can be inverted. ArrayUtils is a class in Apache commons-lang, an open-source library maintained by the Apache Software Foundation. Toying with the object and basic arrays in Java, this intriguing class is used in conjunction with the java.util.Arrays class. The API includes overloaded methods for reversing many types of Java arrays, including int, long, double, float, and object arrays.


WP Ad Inserter plugin for WordPress