Is there an easy way of creating an int[]
or the equivalent Integer[]
from an ArrayList<Integer>
?
When I try (Integer[])list.getArray()
, I get this stack trace:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
The only other way I can think of is:
int[] array = new int[list.size()];
for (int i = 0; i < array.size; i++){
array[i] = list.get(i);
//or I could do list.remove(0), is there any difference?
}
return array;
but that way seems terribly slow.
yourIntList.toArray(new Integer[yourIntList.size()]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.