代码之家  ›  专栏  ›  技术社区  ›  Il-Bhima

将对象数组转换为其基本类型的数组

  •  69
  • Il-Bhima  · 技术社区  · 17 年前

    如果您有一个具有基元类型(例如Byte、Integer、Char等)的Java对象数组。有并没有一种简洁的方法可以将其转换为基元类型的数组?特别是,无需创建新数组并循环内容即可完成此操作。

    例如,给定

    Integer[] array
    

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

    int[] intArray
    

    不幸的是,当在Hibernate和一些我们无法控制的第三方库之间进行接口时,我们必须经常这样做。这似乎是一个非常常见的操作,所以如果没有捷径,我会感到惊讶。

    谢谢你的帮助!

    7 回复  |  直到 12 年前
        2
  •  75
  •   Alex - GlassEditor.com    10 年前

    streams

    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

        3
  •  40
  •   Zach Scrivena    17 年前

    不幸的是,Java平台中没有这样做的东西。顺便说一句,您还需要显式地处理 null 中的元素 Integer[] int 你打算用这些吗?)。

        4
  •  27
  •   Paul Bellora    9 年前

    使用 Guava :

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

    文档:

        5
  •  3
  •   Aaron Digulla    17 年前

    特别是,无需创建新数组并循环内容即可完成此操作。

    在Java中,不能将整数数组转换为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 .

        6
  •  2
  •   Jens Jansson    17 年前

    如果你只想做一次,就用简单的方法来做。但你还没提到整数=空格。

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

    使用 Dollar 简单来说:

    Integer[] array = ...;
    int[] primitiveArray = $(array).toIntArray();
    
        8
  •  0
  •   Sam Ginrich    5 年前

    /**
     * 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);