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.
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.
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.
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 onint[]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 andO(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.

