How to Reverse an Array in Java – 如何在Java中反转一个数组

最后修改: 2017年 12月 24日

1. Overview


In this quick article, we’ll show how we can invert an array in Java.


We’ll see a few different ways to do this using pure Java 8-based solutions – some of those mutate an existing array and some create a new one.

我们将看到使用基于纯Java 8的解决方案来做这件事的几种不同方式–其中一些是突变现有的数组,一些是创建一个新的数组。

Next, we’ll look at two solutions using external libraries — one using Apache Commons Lang and one using Google Guava.

接下来,我们将看看两个使用外部库的解决方案–一个使用Apache Commons Lang,一个使用Google Guava

2. Defining the Problem


The basic idea is to reverse the order of the elements in the array. So, if given the array:


fruits = {"apples", "tomatoes", "bananas", "guavas", "pineapples"}

We’d like to get:


invertedFruits = {"pineapples", "guavas", "bananas", "tomatoes",  "apples"}

Let’s see some ways we can do that.


3. Using a Traditional for Loop


The first way we might think about to invert an array is by using a for loop:


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

As we can see, the code iterates through half of the array, changing the elements in symmetric positions.


We use a temporary variable so that we don’t lose the value of the current position of the array during the iteration.


4. Using Java 8 Stream API

4.使用Java 8的Stream API

We can also invert an array by using the Stream API:

我们也可以通过使用Stream API来反转一个数组。

Object[] invertUsingStreams(Object[] array) {
    return IntStream.rangeClosed(1, array.length)
      .mapToObj(i -> array[array.length - i])

Here we use the method IntStream.range to generate a sequential stream of numbers. Then we map this sequence into array indexes in descending order.


5. Using Collections.reverse()


Let’s see how to invert an array using the Collections.reverse() method:


public void invertUsingCollectionsReverse(Object[] array) {
    List<Object> list = Arrays.asList(array);

Compared with the previous examples, this is a more readable way to do the task.


6. Using Apache Commons Lang

6.使用Apache Commons Lang

Another option to invert an array is to use the Apache Commons Lang library. To use it, we must first include the library as a dependency:

另一种反转数组的方法是使用Apache Commons Lang库。要使用它,我们必须首先将该库作为一个依赖项。


The latest version of Commons Lang can be found at Maven Central.

最新版本的Commons Lang可以在Maven Central找到。

Let’s use the ArrayUtils class to invert the array:


public void invertUsingCommonsLang(Object[] array) {

As we can see, this solution is quite simple.


7. Using Google Guava

7.使用Google Guava

One more option is to use the Google Guava library. Just as we did with the Commons Lang, we’ll include the library as a dependency:

还有一个选择是使用Google Guava库。就像我们对Commons Lang所做的那样,我们将把这个库作为一个依赖项。


The latest version can be found at Maven Central.

最新版本可在Maven Central找到。

Then, we can use the reverse method in Guava’s Lists class to invert the array:


public Object[] invertUsingGuava(Object[] array) {
    List<Object> list = Arrays.asList(array);
    List<Object> reversed = Lists.reverse(list);
    return reversed.toArray();

8. Conclusion


In this article, we looked at several different ways to invert an array in Java. We showed a few solutions using only core Java and two other solutions that use third-party libraries — Commons Lang and Guava.

在这篇文章中,我们研究了几种在Java中反转数组的不同方法。我们展示了一些只使用核心Java的解决方案和另外两个使用第三方库的解决方案–Commons LangGuava

All the code samples shown here can be found on GitHub — this is a Maven project, so it should be easy to import and run as it is.