3 Ways to Reverse an Array in Java - Coding Interview Question

One of the common coding questions is, how do you reverse an array in Java? Well, there are multiple ways to solve this problem. You can reverse array by writing your own function, which loops through the array and swaps elements until the array is sorted. That's actually should you be your first approach on interviews. Later you can impress the interviewer by a couple of other tricks, which is specific to Java development world. For example, you can reverse an array by converting array to ArrayList and then use this code to reverse the ArrayList. You can also use Apache Commons ArrayUtils.reverse() method to reverse any array in Java. This method is overloaded to reverse byte, short, long, int, float, double and String array. You can use any of the method depending upon your array type.

Solution 1 - Revere array in Place

This is one of the simplest ways to reverse an array in Java. This algorithm iterate over array and swap elements until you reach the midpoint. This is also known as reversing an array in-place because no additional buffer is used.

for(int i=0; i<array.length/2; i++){
  int temp = array[i];
  array[i] = array[array.length -i -1];
  array[array.length -i -1] = temp;

The time complexity of this algorithm is O(n/2) which is O(N) because we are iterating over array till midpoint only. This should be your solution on interviews, rest of two methods are for practical use purpose.

Btw, if you are preparing for Java interviews and solving coding questions to gain confidence, then you should also check Java Programming Interview exposed, one of the must read book to do well on Java Interviews.

Solution 2 - Using ArrayList

Another simple way to reverse an array in Java is by first converting the array to List and then using Collections.reverse() method which takes a List and reverse the element in linear time. You should use this approach if you need to reverse an array in your project. You can reverse int, String or any type of array by using this method. Let's see an example of reversing a String array in Java:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

 * Simple Java Program to reverse an array
 * @author Javin Paul
public class ArrayReverse {

    public static void main(String args[])  {
      String[] typesOfInsurance = {"Life Insurance", "Car Insurance", "Health Insurance"};
      System.out.println("array before reverse: " + Arrays.toString(typesOfInsurance) );
      List<String> listOfProducts = Arrays.asList(typesOfInsurance);      
      String[] reversed = listOfProducts.toArray(typesOfInsurance);
      System.out.println("array after reverse: " + Arrays.toString(reversed) );


array before reverse: [Life Insurance, Car Insurance, Health Insurance]
array after reverse: [Health Insurance, Car Insurance, Life Insurance]

You can see the order of elements are reverse in the  final array returned by toArray() method of List class.

Btw, if you are struggling with algorithms and data structure then please check the new Grokking Algorithm by Aditya Bhargava. One of the best algorithm books for beginners. He has done a great job on explaining various algorithms with real world examples.

3 Ways to Reverse an Array in Java - Coding Interview Question

I know learning algorithms is not easy and the books we have  e.g. Introduction of Algorithms and Algorithms 4th edition is also not very easy for beginners, hence I am promoting this book to all beginner programmers. The good knowledge of data structure and algorithms goes a long way in your career and it doesn't matter whether you do coding in Java or C++, these concept remain same.

Solution 3 - By using ArrayUtils.reverse()

Apache commons is an open source library which provides several utility libraries which are essential for software development in Java. In fact, one should by default add this library into their Java projects to complement JDK. Apache commons lang provides an ArrayUtils class which has overloaded reverse() methods to reverse int, float or object arrays in Java. This method also reverses the given array in place i.e. it doesn't return a new array.

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

 * Java Program to reverse an array using Apache Commons Lang ArrayUtils
 * class.
 * @author Javin Paul
public class Pattern {

    public static void main(String args[])  {
      String[] assetClasses = {"bond", "equity", "gold", "real estate"};
      System.out.println("Array before reversing: " + Arrays.toString(assetClasses));
      System.out.println("Array after reversing: " + Arrays.toString(assetClasses));


Array before reversing: [bond, equity, gold, real estate]
Array after reversing: [real estate, gold, equity, bond]

You can see we have manged to reverse the array in just one line now. The ArrayUtils class is from Apache commons lang and you need to add commons-lang3-3.4.jar into your application's classpath. Alternatively, if you are using Maven then you can also add following dependency in your pom.xml file.


That's all about how to reverse an array in Java. You have learned three different ways to solve this problem, first, you can the in-place algorithm to reverse array if you were asked to solve this problem on interviews. Second, you can use the ArrayList class if you need to reverse the array in your project and last, you can use utility method ArrayUtils.reverse() from Apache commons lang library if your project is already using it. If you want more of such questions from tech interviews, please see Cracking the Coding Interview 6th Edition, it contains over 190 coding questions from different software companies, startups, investment banks, and service based companies.

Other array based coding problems for Java developers
  • How to implement binary search in an array? (solution)
  • How to find all pairs in array whose sum is equal to k (solution)
  • How to reverse an array in place in Java? (solution)
  • How to check if an array contains a particular value? (solution)
  • How to sort an array using bubble sort in Java? (solution)
  • How to find duplicates from an unsorted array in Java? (solution)
  • How to remove duplicates from an array in Java? (solution)
  • How to find the largest and smallest number in an array without sorting? (solution)
  • How to find one missing number in  a sorted array? (solution)
  • How to remove an element from an array in Java? (solution)
  • How to find top 2 numbers from given array? (solution)

Array data structure


  1. We can write following way also.

    package com.main.java.resources;

    public class ArraysReverse {
    public static void main(String[] args) {
    String array[] = { "test1", "test2", "test3" };
    String arrayTemp[]=new String[array.length];
    for (int i = arrayTemp.length-1; i >= 0; i--) {
    for (int j = i; j <=i; j++) {

    1. No, you're not reversing anything. You are just printing it out reversed. That's not about what this article is saying. Also, your approach have a O(N*2) complexity and creating N arrays which is very inefficient.