82

如果您有一组具有原始类型(例如字节、整数、字符等)的 Java 对象。有没有一种巧妙的方法可以将其转换为原始类型的数组?特别是,这可以在不必创建新数组并循环内容的情况下完成。

例如,给定

Integer[] array

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

int[] intArray

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

谢谢你的帮助!

4

8 回答 8

91

Apache Commons Lang再次成为您的朋友。他们提供了 ArrayUtils.toPrimitive(),这正是您所需要的。您可以指定要如何处理空值。

于 2009-02-19T09:07:54.473 回答
77

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

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

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

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

或者,如果您愿意,可以将 for 循环替换为流:

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

NullPointerException如果您的任何元素是,所有这些都会抛出null.

于 2014-07-13T06:27:42.800 回答
40

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

于 2009-02-19T08:37:31.190 回答
27

使用番石榴

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

文档:

于 2013-09-13T20:05:31.200 回答
3

特别是,这可以在不必创建新数组并循环内容的情况下完成。

在 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(); }
}

这可以使您的代码更具可读性,并且 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();
    }
}

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

于 2009-02-19T08:41:46.110 回答
2

或者如果你只做一次,就用简单的方法做。但是您还没有谈到 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++;
    }
于 2009-02-19T08:46:38.830 回答
0

使用美元很简单:

Integer[] array = ...;
int[] primitiveArray = $(array).toIntArray();
于 2010-02-01T20:17:47.490 回答
0

这是所有原始类型的通用解决方案

/**
 * Convert Collection to equivalent array of primitive type
 * @param <S> [in] Object type of source collection
 * @param tcls [in] class of the primitive element
 * @param q [in] source collection
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, Collection<S> q)
{
    int n = q.size();
    Object res = Array.newInstance(tcls, n);
    Iterator<S> i = q.iterator();
    int j = 0;
    while (i.hasNext())
    {
        Array.set(res, j++, i.next());
    }
    return res;
}

/**
 * Convert Object array to equivalent array of primitive type
 * @param <S> [in] Object type of source array
 * @param tcls [in] class of the primitive element
 * @param s [in] source array
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, S[] s)
{
    return asPrimitiveArray(tcls, Arrays.asList(s));
} 

对于整数到整数的转换

Integer[] a = ...
int[] t = (int[]) asPrimitiveArray(int.class, a);
于 2021-07-14T17:28:06.057 回答