Sorting Algorithms ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-23

How to Reverse an Array in Java

To reverse an array in Java (also called inverting or flipping an array) means arranging its elements in the opposite order: the first element becomes the last, and the last becomes the first. Given {1, 2, 3, 4}, we want to produce {4, 3, 2, 1}. In this lesson we'll cover how to reverse an array in place (without extra memory), how to build a reversed copy without touching the original, and which built-in tools the standard Java library offers.

Reverse an array in place

The most efficient way to reverse an array is to swap symmetric elements without allocating anything extra. We find the middle of the array — array.length / 2 — then iterate from the start to the middle, swapping the element at index i with the element at array.length - i - 1. A temporary variable holds one value during the swap:

public class ArrayInverter {
    public static void invert(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            int tmp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i - 1] = tmp;
        }
    }
}

The loop only walks through half of the array: elements past the midpoint are already placed during the first iterations. If the length is odd, the center element stays where it is — there's nothing to swap it with. This algorithm runs in O(n) time and uses O(1) extra memory.

Note that the ArrayInverter class has no main() method — we'll call invert() from another class. To call a method from a different class, write the class name before the method name: ArrayInverter.invert(array1).

Good to know

The Java standard library has no built-in Arrays.reverse() for arrays — unlike Collections.reverse() for lists. That's why reversing an array of primitives is usually written by hand with a loop, as shown above, or done with ArrayUtils.reverse() from the third-party Apache Commons Lang library.

Testing the method on different arrays

Let's test invert() on edge cases: an empty array, a single element, and both even and odd lengths. Arrays.toString() is handy for printing an array:

import java.util.Arrays;

public class ArrayInverterExample1 {
    public static void main(String[] args) {
        int[] array1 = new int[]{};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1, 2};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));

        array1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.print(Arrays.toString(array1) + " => ");
        ArrayInverter.invert(array1);
        System.out.println(Arrays.toString(array1));
    }
}

Output:

[] => []
[0] => [0]
[0, 1] => [1, 0]
[0, 1, 2] => [2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] => [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The method handles both an empty array and a single-element array correctly: in both cases array.length / 2 equals zero, so the loop never runs.

Removing code duplication

Looking closely at ArrayInverterExample1, you can see the same block of code repeated five times. Duplication is bad practice: any change would have to be made in five places. Let's extract the repeated logic into a separate testInvert() method and call it with different arrays:

import java.util.Arrays;

public class ArrayReverseExample {
    public static void main(String[] args) {
        testInvert(new int[]{});
        testInvert(new int[]{0});
        testInvert(new int[]{0, 1});
        testInvert(new int[]{0, 1, 2});
        testInvert(new int[]{0, 1, 2, 3, 4});
    }

    private static void testInvert(int[] array) {
        System.out.print(Arrays.toString(array) + " => ");
        ArrayInverter.invert(array);
        System.out.println(Arrays.toString(array));
    }
}

The code is now shorter and clearer, and the printing logic lives in one place that's easy to change.

Reverse without changing the original

The invert() method modifies the source array — this is called an in-place change. Sometimes you need to keep the original and get a reversed version separately. In that case, create a new array and fill it in reverse order:

public static int[] reversedCopy(int[] source) {
    int[] result = new int[source.length];
    for (int i = 0; i < source.length; i++) {
        result[i] = source[source.length - 1 - i];
    }
    return result;
}

Here the input array source stays untouched and the reversed result is returned as a new array. This approach needs O(n) extra memory but never corrupts the input data.

Reverse with Stream (IntStream)

For an int[] you can reverse elements declaratively with IntStream, iterating indices from the end:

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayStreamReverse {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5};
        int[] reversed = IntStream.rangeClosed(1, source.length)
                .map(i -> source[source.length - i])
                .toArray();
        System.out.println(Arrays.toString(reversed)); // [5, 4, 3, 2, 1]
    }
}

The Stream version reads more compactly, but for primitives it's slower than a plain loop and always allocates a new array. In interviews, the manual in-place reversal is usually what's expected.

Collections.reverse for a List

If your data lives in a List rather than an array, there's no need to reverse it by hand — the built-in Collections.reverse() method reverses the list in place:

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

public class ListReverse {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        Collections.reverse(numbers);
        System.out.println(numbers); // [4, 3, 2, 1]
    }
}

Keep in mind: Collections.reverse() works only with lists, not with arrays. A primitive array int[] can't be turned into a List<Integer> directly via Arrays.asList() — more on that in the common mistakes section below.

Arrays of strings and objects

The same swap algorithm works for an array of any objects — you just change the type. Let's make the method generic so it can reverse a String[], an Integer[], or any other reference type:

public static <T> void invert(T[] array) {
    for (int i = 0; i < array.length / 2; i++) {
        T tmp = array[i];
        array[i] = array[array.length - i - 1];
        array[array.length - i - 1] = tmp;
    }
}

Generics don't work with primitives, so keep a separate version with int for int[] and use the generic method for arrays of objects. For example, invert(new String[]{"a", "b", "c"}) turns the array into {"c", "b", "a"}.

Common mistakes

  • Looping to the end instead of the middle: if you iterate over all elements (i < array.length), the array gets reversed and then reversed back to its original order. The boundary must be array.length / 2.
  • Worrying about the middle element: with an odd length the center element doesn't need to be moved — it already stays in place. Looping to the middle handles this automatically.
  • Collections.reverse on an array: the method takes a List, not an array — it won't compile for an int[].
  • Arrays.asList with a primitive array: Arrays.asList(intArray) returns a List containing a single element — the array itself — not a list of numbers. Use an Integer[] wrapper array or streams instead.

Frequently asked questions

How do I reverse an array in Java without creating a new one?

Swap symmetric elements in a loop that runs from the start to the middle: the element at index i and the element at array.length - i - 1, using a temporary variable. This is an in-place reversal — O(n) time and no extra memory.

Does Java have a built-in method to reverse an array?

No, there is no Arrays.reverse() in the standard library. Lists have Collections.reverse(), and for arrays you can use ArrayUtils.reverse() from Apache Commons Lang or write the reversal by hand with a loop.

How do I reverse an array without changing the original?

Create a new array of the same length and fill it by walking the source array from the end: result[i] = source[source.length - 1 - i]. The original array stays unchanged and the reversed copy is returned separately.

How do I reverse an array of strings or objects?

Use the same swap algorithm with a generic method: public static <T> void invert(T[] array). It reverses String[], Integer[], and any reference type. Generics don't apply to int[], so primitives need a separate version.

Comments

Please log in or register to have a possibility to add comment.