简体   繁体   English

(取消)在Java中装箱原始数组

[英](Un)boxing primitive arrays in Java

in the Android Java world, is there a straighforward (ideally one-call) way to convert an array of int to an ArrayList<Integer> and back? 在Android Java世界中,是否存在将int数组转换为ArrayList<Integer>并返回的直观方法(最好是一次调用)? Calling toArray() on the ArrayList returns an array of Integer - not quite what I want. 在ArrayList上调用toArray()返回一个Integer数组-不完全是我想要的。

I can easily do that by hand with a loop (already did, in fact). 我可以轻松地手动执行一个循环(实际上已经做到了)。 I'm wondering if the library/language supports the same. 我想知道库/语言是否支持相同的功能。

EDIT: thanks all, I already wrote my own boxer and unboxer. 编辑:谢谢,我已经写了自己的拳击手和拆箱手。 I'm just surprised the language/RTL designers didn't think of it themselves, especially with primitive types being by design ineligible for collections. 我感到惊讶的是语言/ RTL设计师自己没有想到它,尤其是原始类型在设计上不符合集合的条件。

Using Guava ( Ints.asList(int...) and Ints.toArray(Collection<Integer>) ): 使用番石榴Ints.asList(int ...)Ints.toArray(Collection <Integer>) ):

int[] intArray = ...
List<Integer> intArrayAsList = Ints.asList(intArray);
int[] intArray2 = Ints.toArray(intArrayAsList);

Note that like Arrays.asList , Ints.asList returns a list that is a view of the given array. 请注意,像Arrays.asList一样, Ints.asList返回一个列表,该列表是给定数组的视图 You can't add to or remove from it, but you can set the value at a specific index. 您不能对其进行添加或删除,但是可以setset为特定的索引。 You can also copy it to a new ArrayList if you want: 您还可以根据需要将其复制到新的ArrayList

List<Integer> arrayList = Lists.newArrayList(Ints.asList(intArray));

Guava has the same methods for all primitive types. 番石榴对所有原始类型都具有相同的方法。

If you create an utility method that would be a one call conversion. 如果创建的实用程序方法将是一次调用转换。 Perhaps in your case that would be better than adding a whole library ( of which you might just use one function ) although that should be too much burden for your app. 也许在您的情况下,这比添加整个库(您可能只使用一个函数)更好,尽管这对您的应用程序来说负担太大。

Here's the source code for the h3xStream answer ( glad it uses Apache license ) ;) 这是h3xStream答案的源代码 (很高兴它使用了Apache license);)

Boxing and unboxing (without special libraries): 装箱和拆箱(无特殊库):

// Necessary Imports:
import java.util.*;
import java.util.stream.*;

// Integers:
int[] primitives = {1, 2, 3};
Integer[] boxed = Arrays.stream(primitives).boxed().toArray(Integer[]::new);
int[] unboxed = Arrays.stream(boxed).mapToInt(Integer::intValue).toArray();

// Doubles:
double[] primitives = {1.0, 2.0, 3.0};
Double[] boxed = Arrays.stream(primitives).boxed().toArray(Double[]::new);
double[] unboxed = Arrays.stream(boxed).mapToDouble(Double::doubleValue).toArray();

Note that this requires Java 8 ( Stream Documentation ). 请注意,这需要Java 8流文档 )。

For anyone unaware of what the :: means, it's used to make a Method Reference . 对于任何不知道::含义的人,它都用作方法参考

I managed to find a solution in Java 8 : 我设法在Java 8中找到一个解决方案:

import java.util.Arrays;
...
double[] columnsValue = {...};
Double[] values = Arrays.stream(columnsValue).boxed().toArray(Double[]::new);

I can't explain what Double[]::new means exactly, because I didn't know this feature of Java 8 before (in fact, IntelliJ IDEA wrote this for me), but it looks like it is doing new Double(double) on every element of the array. 我无法解释Double[]::new到底是什么意思,因为我以前不了解Java 8的这一功能(实际上, IntelliJ IDEA为我编写了此功能),但看起来它正在做new Double(double)在数组的每个元素上。

Please note : I tested this only on desktop, I don't know if it works in Android. 请注意 :我只在台式机上测试过,不知道它是否可以在Android上运行。

来自Apache的Common.Lang有一个执行该任务的实用程序类: ArrayUtils

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM