1. Introduction
1.绪论
In this short tutorial, we’ll show how to convert an array of primitives to an array of objects, and vice versa.
在这个简短的教程中,我们将展示如何将一个基元数组转换成一个对象数组,反之亦然。
2. Problem
2 问题
Let’s say we have an array of primitives, such as int[], and we would like to convert it to an array of objects, Integer[]. We might intuitively try casting:
假设我们有一个基元数组,比如int[],我们想把它转换成一个对象数组,Integer[]。我们可能会凭直觉尝试铸造。
Integer[] integers = (Integer[])(new int[]{0,1,2,3,4});
However, this will result in a compilation error because of inconvertible types. That’s because autoboxing only applies to individual elements and not to arrays or collections.
然而,这将导致编译错误,因为类型不可逆。这是因为autoboxing只适用于单个元素而不适用于数组或集合。
Therefore, we need to convert the elements one by one. Let’s take a look at a couple of options to do that.
因此,我们需要逐一转换这些元素。让我们来看看有几个选项可以做到这一点。
3. Iteration
3.迭代
Let’s see how we can use autoboxing in an iteration.
让我们看看如何在一个迭代中使用自动排版。
First, let’s convert from a primitive array to an object array:
首先,让我们从原始数组转换为对象数组。
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = new Integer[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
}
assertArrayEquals(expected, output);
Now, let’s convert from an array of objects to an array of primitives:
现在,让我们从一个对象数组转换为一个基元数组。
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
}
assertArrayEquals(expected, output);
As we can see, this is not complicated at all, but a more readable solution, like the Stream API, might suit our needs better.
正如我们所看到的,这一点也不复杂,但一个更可读的解决方案,如Stream API,可能更适合我们的需要。
4. Streams
4.溪流
Since Java 8, we can use the Stream API to write fluent code.
从Java 8开始,我们可以使用Stream API来编写流畅的代码。
First, let’s see how to box the elements of a primitive array:
首先,让我们看看如何对一个原始数组的元素进行装箱。
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = Arrays.stream(input)
.boxed()
.toArray(Integer[]::new);
assertArrayEquals(expected, output);
Notice the Integer[]::new parameter in the toArray method. Without this parameter, the stream would return an Object[] instead of the Integer[].
注意在toArray方法中的Integer[]::new参数。如果没有这个参数,流会返回一个Object[]而不是Integer[]。
Next, to convert them back, we’ll use the mapToInt method together with the unboxing method of Integer:
接下来,为了将它们转换回来,我们将使用mapToInt方法和Integer的unboxing方法。
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = Arrays.stream(input)
.mapToInt(Integer::intValue)
.toArray();
assertArrayEquals(expected, output);
With the Stream API, we created a more readable solution, but if we still wish it were more concise, we could try a library, like Apache Commons.
通过Stream API,我们创建了一个更可读的解决方案,但如果我们仍然希望它更简明,我们可以尝试一个库,比如Apache Commons。
5. Apache Commons
5.阿帕奇公社
First, let’s add the Apache Commons Lang library as a dependency:
首先,让我们把Apache Commons Lang库作为一个依赖项加入。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Then, to convert a primitives array to its boxed counterpart, let’s use the ArrayUtils.toObject method:
然后,为了将一个基元数组转换为盒状的对应物,让我们使用ArrayUtils.toObject方法。
int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };
Integer[] output = ArrayUtils.toObject(input);
assertArrayEquals(expected, output);
Lastly, to convert back the boxed elements to primitives, let’s use the ArrayUtils.toPrimitives method:
最后,为了将盒装元素转换回基元,让我们使用ArrayUtils.toPrimitives方法。
Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };
int[] output = ArrayUtils.toPrimitive(input);
assertArrayEquals(expected, output);
The Apache Commons Lang library provides a concise, easy-to-use solution to our problem, with the cost of having to add a dependency.
Apache Commons Lang库为我们的问题提供了一个简明、易用的解决方案,但代价是必须增加一个依赖关系。
6. Conclusion
6.结语
In this article, we’ve looked at a couple of ways to convert an array of primitives to an array of their boxed counterparts, and then, convert the boxed elements back to their primitive counterparts.
在这篇文章里,我们看了几种将基元数组转换为其盒式对应物数组的方法,然后,将盒式元素转换回其基元对应物。
As always, the code examples of this article are available over on GitHub.
一如既往,本文的代码示例可在GitHub上获得超过。