Java Basic Examples

Java Reverse Array Example

Java Reverse Array Example shows how to reverse an array in Java. This example also shows how to reverse array without temporary array, reverse array using Collections and reverse array using Apache commons library.

How to reverse array in Java?

There are several ways in which you can reverse array in Java.

1) Reverse array using Collections and Arrays classes

If you want to reverse an object array, you can use reverse method of Collections class and asList method of Arrays class.

asList method of Arrays class returns List created on the top of the array. reverse method reverses a List as given below.



This approach does not work for an array of primitive values for example array of int.

2) Reverse array using second array

An array can be reversed with the help of the second array and a for loop as given below.


3) Reverse array using for loop by swapping elements

You can avoid using the second array and do the in-place swapping to reverse an array as given below.


Basically, we are swapping the first half of the elements of an array with the second half to reverse it.

4) Reverse array using Apache Commons

If you have Apache Commons library, you can use static reverse method of ArrayUtils class to reverse an array. reverse method has been overloaded for all the primitive types as well as for an Object.

a) Reverse int array


b) Reverse String array


Please let us know your views in the comments section below.


Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.