Java
Array Manipulation
Programming
Reverse Array
Data Structures

How do I reverse an int array in Java?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Reversing an int[] in Java usually means swapping elements from both ends until you reach the middle. The important design choice is whether you want to reverse the array in place or create a new reversed copy, because those two versions have different memory and mutation behavior.

Core Sections

In-place reversal is the standard answer

For most Java code, the simplest and most efficient approach is to swap the first and last elements, then move inward.

java
1import java.util.Arrays;
2
3public class Main {
4    static void reverseInPlace(int[] values) {
5        int left = 0;
6        int right = values.length - 1;
7
8        while (left < right) {
9            int temp = values[left];
10            values[left] = values[right];
11            values[right] = temp;
12            left++;
13            right--;
14        }
15    }
16
17    public static void main(String[] args) {
18        int[] numbers = {1, 2, 3, 4, 5};
19        reverseInPlace(numbers);
20        System.out.println(Arrays.toString(numbers));
21    }
22}

This runs in O(n) time and uses O(1) extra space, which is why it is the usual interview and production answer.

Sometimes you want a reversed copy instead

If other code still needs the original order, mutate a new array instead of the existing one.

java
1import java.util.Arrays;
2
3public class Main {
4    static int[] reversedCopy(int[] values) {
5        int[] result = new int[values.length];
6        for (int i = 0; i < values.length; i++) {
7            result[i] = values[values.length - 1 - i];
8        }
9        return result;
10    }
11
12    public static void main(String[] args) {
13        int[] numbers = {1, 2, 3, 4, 5};
14        int[] reversed = reversedCopy(numbers);
15
16        System.out.println(Arrays.toString(numbers));
17        System.out.println(Arrays.toString(reversed));
18    }
19}

This still takes O(n) time, but it uses O(n) extra space because it allocates a second array.

Why collections utilities do not help directly

Java’s Collections.reverse() works on List objects, not on primitive arrays such as int[]. That is why this is not a direct solution for primitive arrays.

If you convert int[] to a list incorrectly, you will often end up with a list containing one array object instead of a list of integers.

That is one reason the manual swap loop is still the most straightforward answer for primitive arrays.

Recursion works, but it is rarely the best choice

You can reverse recursively, but it adds call-stack overhead without improving clarity or performance for this problem.

java
1import java.util.Arrays;
2
3public class Main {
4    static void reverseRecursive(int[] values, int left, int right) {
5        if (left >= right) {
6            return;
7        }
8
9        int temp = values[left];
10        values[left] = values[right];
11        values[right] = temp;
12
13        reverseRecursive(values, left + 1, right - 1);
14    }
15
16    public static void main(String[] args) {
17        int[] numbers = {1, 2, 3, 4, 5};
18        reverseRecursive(numbers, 0, numbers.length - 1);
19        System.out.println(Arrays.toString(numbers));
20    }
21}

This is fine as an academic exercise, but the iterative approach is usually better in real Java code.

Edge cases are simple but worth handling mentally

The swap-loop version already handles these correctly:

  • empty array
  • one-element array
  • even-length array
  • odd-length array

That is one of the nice things about the two-pointer approach. Once the loop condition is correct, the boundary cases naturally work out.

Common Pitfalls

  • Expecting Collections.reverse() to work directly on int[] ignores the difference between primitive arrays and Java collections.
  • Reversing in place when callers still need the original ordering can create hard-to-trace side effects.
  • Writing a copy-based solution when in-place mutation is acceptable wastes memory unnecessarily.
  • Getting the loop condition or right-side index wrong causes off-by-one bugs that are easy to miss on small test cases.
  • Using recursion for very large arrays adds call-stack overhead without giving any practical advantage over the iterative swap loop.

Summary

  • The standard way to reverse an int[] in Java is a two-pointer in-place swap loop.
  • That approach runs in O(n) time and O(1) extra space.
  • If you need to preserve the original array, build a reversed copy instead.
  • Primitive arrays are not handled directly by Collections.reverse().
  • For most Java code, the iterative solution is the clearest and most practical approach.

Course illustration
Course illustration

All Rights Reserved.