How do I reverse an int array in Java?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How do I run celery status/flower without the -A option?
- How do I search for a number in a 2d array sorted left to right and top to bottom?
- How do I set a number of retry attempts in RabbitMQ?
- How do I shuffle an array in Swift?
- How do I run a Java program from the command line on Windows?
- How do I run a spring boot executable jar in a Production environment?
- How do I sort a dictionary by key?
- How do I sort a dictionary by key?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.