[英]Casting primitive data type array to an OO array
Is there an elegant, clean way for converting primitive data type double
array to OO Double
array (eg double[]
into Double[]
)? 是否有一种优雅,干净的方法将原始数据类型
double
数组转换为OO Double
数组(例如,将double[]
转换为Double[]
)?
There are some nice solutions out there, eg 有一些不错的解决方案,例如
int[] intArray = { ... };
Integer[] what = Arrays.stream( intArray ).boxed().toArray( Integer[]::new );
Integer[] ever = IntStream.of( intArray ).boxed().toArray( Integer[]::new );
(taken from here ), but this requires Java 8 . (从此处获取 ),但这需要Java 8 。 Is there a similar elegant, performant way for Java 7 and below?
对于Java 7和更低版本,是否存在类似的优雅,高效的方式?
If you want a one liner that compiles with Java 7, you can use ArrayUtils from Apache Commons. 如果您想要一个可与Java 7一起编译的内衬,则可以使用Apache Commons中的ArrayUtils 。 Eg
例如
Integer[] what = ArrayUtils.toObject(intArray);
Works for any primitive type. 适用于任何原始类型。 That's probably more "elegant" and "clean" than the Java 8 code you posted.
比您发布的Java 8代码更“优雅”和“简洁”。 If you want to avoid libraries like Apache Commons, I am afraid you have to use a for loop.
如果要避免使用Apache Commons之类的库,恐怕您必须使用for循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.