Reverse an Array in Java

Learn how to reverse or invert an array in Java. A reversed array is of equal size to the original array and contains the same items but in the reverse order.

String[] array          = {"A", "B", "C", "D", "E"};

String[] reveresedArray = {"E", "D", "C", "B", "A"};

1. Collections.reverse() API

The easiest way to reverse the array is to use the existing APIs built for this very purpose. Collections.reverse() method is such an API. This method reverses the elements in a list, so we must convert the array into a list first by using java.util.Arrays.asList(array) and then reverse the list. It will reverse the backing array items as well.

Note that the reverse() method reverses the order of the items in the original array. So if you want to keep the original array unchanged, consider cloning the array first.

String[] array = {"A", "B", "C", "D", "E"};

Collections.reverse(Arrays.asList(array));

System.out.println(Arrays.toString(array)); //[E, D, C, B, A]

Warning: Note that reverse() API does not work with primitive arrays because Arrays.asList(intArray) will return a List<int[]> whose only element is the original array. Thus there is nothing to reverse.

int[] array = {1, 2, 3};

Collections.reverse(Arrays.asList(array));   //Does not reverses the array

2. Swapping Array Elements in For Loop

Another simple and straightforward way is to iterate through the array swapping them from the beginning position with the elements in the last position.

For example, the given image describes the swapping process where we have an array of 5 elements.

  • In the first iteration, we swap the first and last elements.
  • In the second iteration, we swap the second and second-last elements.
  • The same swapping goes on in the for-loop until we hit the middle of the array, at this time the array has been reversed.
String[] array = {"A", "B", "C", "D", "E"};

for (int i = 0; i < array.length / 2; i++) {
    String temp = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = temp;
}

System.out.println(Arrays.toString(array)); //[E, D, C, B, A]

3. Reversing Array Using Stream API

In this approach, we will use read the array elements in reverse starting from last position, and then we will store all the elements in a new array.

The original array remains unchanged in this approach.

final String[] arr = new String[]{"A","B","C","D","E"};

Object[] reversedArr = IntStream.rangeClosed(1, array.length)
    .mapToObj(i -> arr[arr.length - i])
    .toArray();

System.out.println(Arrays.toString(reversedArr)); //[E, D, C, B, A]

4. ArrayUtils.reverse()

If our application has an existing dependency on Apache Commons Lang library then it has an excellent and direct API to reverse any kind of array.

String[] array = {"A", "B", "C", "D", "E"};

ArrayUtils.reverse(arr); 

System.out.println(Arrays.toString(arr)); //[E, D, C, B, A]

5. Conclusion

In this short tutorial, we learned to reverse an array using different techniques. We learned to use for-loop, swapping items, Collections API and also the Apache Commons’s ArrayUtils class.

In any of the approaches, if you want to keep the original array unchanged, clone the array first and then apply the reverse logic to it.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode