我试图用下面的代码段将包含整数对象的数组列表转换为原始int[],但它抛出编译时错误。可以在Java中转换吗?

List<Integer> x =  new ArrayList<Integer>();
int[] n = (int[])x.toArray(int[x.size()]);

当前回答

Arrays.setAll()适用于大多数场景:

Integer List to primitive int array: public static int[] convert(final List<Integer> list) { final int[] out = new int[list.size()]; Arrays.setAll(out, list::get); return out; } Integer List (made of Strings) to primitive int array: public static int[] convert(final List<String> list) { final int[] out = new int[list.size()]; Arrays.setAll(out, i -> Integer.parseInt(list.get(i))); return out; } Integer array to primitive int array: public static int[] convert(final Integer[] array) { final int[] out = new int[array.length]; Arrays.setAll(out, i -> array[i]); return out; } Primitive int array to Integer array: public static Integer[] convert(final int[] array) { final Integer[] out = new Integer[array.length]; Arrays.setAll(out, i -> array[i]); return out; }

其他回答

Apache Commons有一个ArrayUtils类,它有一个方法toPrimitive()来完成这个任务。

import org.apache.commons.lang.ArrayUtils;
...
    List<Integer> list = new ArrayList<Integer>();
    list.add(new Integer(1));
    list.add(new Integer(2));
    int[] intArray = ArrayUtils.toPrimitive(list.toArray(new Integer[0]));

然而,正如Jon所展示的,自己做这件事非常容易,而不是使用外部库。

如果你正在使用java-8,还有另一种方法可以做到这一点。

int[] arr = list.stream().mapToInt(i -> i).toArray();

它的作用是:

从列表中获取一个Stream<Integer> 通过将每个元素映射到自身(标识函数)来获取IntStream,并将每个Integer对象的int值解盒(从Java 5开始自动完成) 通过调用toArray获取int类型的数组

你也可以通过方法引用显式调用intValue,例如:

int[] arr = list.stream().mapToInt(Integer::intValue).toArray();

值得一提的是,如果列表中有任何空引用,则可以获得NullPointerException。这可以通过在流管道中添加一个过滤条件来轻松避免:

                       //.filter(Objects::nonNull) also works
int[] arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray();

例子:

List<Integer> list = Arrays.asList(1, 2, 3, 4);
int[] arr = list.stream().mapToInt(i -> i).toArray(); //[1, 2, 3, 4]

list.set(1, null); //[1, null, 3, 4]
arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray(); //[1, 3, 4]

Arrays.setAll ()

    List<Integer> x = new ArrayList<>(Arrays.asList(7, 9, 13));
    int[] n = new int[x.size()];
    Arrays.setAll(n, x::get);

    System.out.println("Array of primitive ints: " + Arrays.toString(n));

输出:

原始整数数组:[7,9,13]

这同样适用于long或double类型的数组,但不适用于boolean、char、byte、short或float类型的数组。如果您有一个非常大的列表,甚至可以使用parallelSetAll方法来代替。

对我来说,这是足够好的和优雅的,我不想获得一个外部库或使用流。

文档链接:数组。setAll (int [], IntUnaryOperator)

Java 8

int[] array = list.stream().mapToInt(i->i).toArray();

OR 

int[] array = list.stream().mapToInt(Integer::intValue).toArray();
   List<Integer> list = new ArrayList<Integer>();

    list.add(1);
    list.add(2);

    int[] result = null;
    StringBuffer strBuffer = new StringBuffer();
    for (Object o : list) {
        strBuffer.append(o);
        result = new int[] { Integer.parseInt(strBuffer.toString()) };
        for (Integer i : result) {
            System.out.println(i);
        }
        strBuffer.delete(0, strBuffer.length());
    }