[英]how to add element (or convert an Integer ArrayList) to an Integer Array
here is what i have got 这就是我所拥有的
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);
now i want to convert this "list" to an Array... eg: 现在我想将此“列表”转换为数组...例如:
Integer[] actual= {50,55,98,101};
anyway how to do it? 反正怎么办呢? thanks. 谢谢。
Integer[] array = list.toArray(new Integer[list.size()]);
If you want an int[]
array, you'll have to loop over the list and unbox each element explicitly. 如果要使用int[]
数组,则必须遍历列表,并显式取消装箱每个元素。
See http://download.oracle.com/javase/6/docs/api/java/util/List.html next time you're looking for a method of List. 下次您要查找List方法时,请参阅http://download.oracle.com/javase/6/docs/api/java/util/List.html 。
Sefirosu, for another solution you can do it using Arrays.copyOf()
or Arrays.copyOfRange()
too. 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.