[英]How to create ArrayList (ArrayList<Integer>) from array (int[]) in Java
[英]Create an int[] from an ArrayList<Integer>
有沒有一種簡單的方法可以從ArrayList<Integer>
創建int[]
或等效的Integer[]
?
當我嘗試(Integer[])list.getArray()
,我得到了這個堆棧跟蹤:
線程“主”中的異常java.lang.ClassCastException:[Ljava.lang.Object; 無法轉換為[Ljava.lang.Integer;
我能想到的唯一其他方法是:
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;
但是那樣看來似乎很慢。
yourIntList.toArray(new Integer[yourIntList.size()]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.