Java 将对象数组转换为其原始类型数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/564392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:16:56  来源:igfitidea点击:

Converting an array of objects to an array of their primitive types

javaarraysprimitive-types

提问by Il-Bhima

If you have an array of Java objects which have a primitive type (for example Byte, Integer, Char, etc). Is there a neat way I can convert it into an array of the primitive type? In particular can this be done without having to create a new array and loop through the contents.

如果您有一组具有原始类型(例如 Byte、Integer、Char 等)的 Java 对象。有没有一种巧妙的方法可以将其转换为原始类型的数组?特别是可以在不必创建新数组并遍历内容的情况下完成此操作。

So for example, given

例如,给定

Integer[] array

what is the neatest way to convert this into

将其转换为最简洁的方法是什么

int[] intArray

Unfortunately, this is something we have to do quite frequently when interfacing between Hibernate and some third party libraries over which we have no control. It seems this would be a quite common operation so I would be surprised if there's no shortcut.

不幸的是,当在 Hibernate 和一些我们无法控制的第三方库之间进行交互时,这是我们必须经常做的事情。看起来这将是一个很常见的操作,所以如果没有捷径,我会感到惊讶。

Thanks for your help!

谢谢你的帮助!

采纳答案by Zach Scrivena

Unfortunately, there's nothing in the Java platform that does this. Btw, you also need to explicitly handle nullelements in the Integer[]array (what intare you going to use for those?).

不幸的是,Java 平台中没有任何东西可以做到这一点。顺便说一句,您还需要显式处理数组中的null元素Integer[]int您打算对这些元素使用什么?)。

回答by Aaron Digulla

In particular can this be done without having to create a new array and loop through the contents.

特别是可以在不必创建新数组并遍历内容的情况下完成此操作。

You can't convert an array of Integer to int (i.e. you can't change the type of the elements of an array) in Java. So you either must create a new int[] array and copy the value of the Integer objects into it or you can use an adapter:

在 Java 中不能将 Integer 数组转换为 int(即不能更改数组元素的类型)。因此,您必须创建一个新的 int[] 数组并将 Integer 对象的值复制到其中,或者您可以使用适配器:

class IntAdapter {
    private Integer[] array;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { return array[index].intValue(); }
}

This can make your code a little more readable and the IntAdapter object will only consume a few bytes of memory. The big advantage of an adapter is that you can handle special cases here:

这可以使您的代码更具可读性,并且 IntAdapter 对象将只消耗几个字节的内存。适配器的一大优势是您可以在此处处理特殊情况:

class IntAdapter {
    private Integer[] array;
    public int nullValue = 0;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { 
        return array[index] == null ? nullValue : array[index].intValue();
    }
}

Another solution is to use Commons Primitiveswhich contains lots of predefined adapters. In your case, have a look at ListIntList.

另一种解决方案是使用包含许多预定义适配器的Commons Primitives。在您的情况下,请查看ListIntList

回答by Jens Jansson

Or just do it the easy way if you gonna do it only once. But you haven't talked about Integer!=null case.

或者如果你只做一次,就用简单的方法来做。但是你还没有谈到 Integer!=null 的情况。

    //array is the Integer array
    int[] array2 = new int[array.length];
    int i=0;
    for (Integer integer : array) {
        array2[i] = integer.intValue();
        i++;
    }

回答by Guillaume

Once again, Apache Commons Langis your friend. They provide ArrayUtils.toPrimitive()which does exactly what you need. You can specify how you want to handle nulls.

Apache Commons Lang再次成为您的朋友。他们提供了ArrayUtils.toPrimitive()来满足你的需求。您可以指定处理空值的方式。

回答by dfa

using Dollaris simple as:

使用Dollar很简单:

Integer[] array = ...;
int[] primitiveArray = $(array).toIntArray();

回答by Paul Bellora

Using Guava:

使用番石榴

int[] intArray = Ints.toArray(Arrays.asList(array));

Documentation:

文档:

回答by Alex

With streamsintroduced in Java 8 this can be done:

使用Java 8 中引入的可以做到这一点:

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();

However, there are currently only primitive streams for int, longand double. If you need to convert to another primitive type such as bytethe shortest way without an external library is this:

但是,目前只有int,long和 的原始流double。如果您需要转换为另一种基本类型,例如byte没有外部库的最短方法是这样的:

byte[] byteArray = new byte[array.length];
for(int i = 0; i < array.length; i++) byteArray[i] = array[i];

Or the for loop can be replaced with a stream if you want:

或者,如果需要,可以将 for 循环替换为流:

IntStream.range(0, array.length).forEach(i -> byteArray[i] = array[i]);

All of these will throw a NullPointerExceptionif any of your elements are null.

所有这些都将抛出一个NullPointerException,如果你的任何元素都是null