Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将元素(或转换整数数组列表)添加到整数数组_Java_Arrays_Arraylist_Integer - Fatal编程技术网

Java 如何将元素(或转换整数数组列表)添加到整数数组

Java 如何将元素(或转换整数数组列表)添加到整数数组,java,arrays,arraylist,integer,Java,Arrays,Arraylist,Integer,这是我得到的 ArrayList<Integer> list = new ArrayList<Integer>(); Integer a = 50; Integer b = 55; Integer c = 98; Integer d = 101; list.add(a); list.add(b); list.add(c); list.add(d); 不管怎样,怎么做?谢谢 Integer[] array

这是我得到的

ArrayList<Integer> list = new ArrayList<Integer>();
    Integer a = 50; 
    Integer b = 55;
    Integer c = 98;
    Integer d = 101;
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);
不管怎样,怎么做?谢谢

Integer[] array = list.toArray(new Integer[list.size()]);
如果需要
int[]
数组,则必须在列表上循环并显式取消对每个元素的装箱

下次在寻找列表方法时,请参见

如果需要
int[]
数组,则必须在列表上循环并显式取消对每个元素的装箱


下次查找列表方法时,请参阅。

Sefirosu,对于另一种解决方案,您也可以使用
Arrays.copyOf()
Arrays.copyOfRange()
来完成

Integer[] integerArray = Arrays.copyOf(list.toArray(), list.toArray().length, Integer[].class);
Integer[] integerArray = Arrays.copyOfRange(list.toArray(), 0, 4, Integer[].class);

Sefirosu,对于另一个解决方案,您也可以使用
Arrays.copyOf()
Arrays.copyOfRange()
来实现

Integer[] integerArray = Arrays.copyOf(list.toArray(), list.toArray().length, Integer[].class);
Integer[] integerArray = Arrays.copyOfRange(list.toArray(), 0, 4, Integer[].class);
list.toArray(新整数[0]);list.toArray(新整数[0]);Apache方法可以帮助在包装器数组和原语数组之间进行转换。Apache方法可以帮助在包装器数组和原语数组之间进行转换。