2012-02-22 86 views
0

我正在處理測驗(作業)的一部分,我需要將值從ArrayList []個位置傳遞到int []的一部分intArray 。這是如何完成的?謝謝。如何將ArrayList []中的所有值傳遞給int [] intArray

1. ArrayList locations = new ArrayList(); 
2. int anArray = locations.size(); 
3. int[] intArray = new int[anArray]; 

謝謝

+0

重複的:http://stackoverflow.com/questions/2745338/convert-an-arraylist-to-an-object-array – 2012-02-22 00:43:37

回答

0

他的意思呢?

ArrayList<Integer> locations = new ArrayList<Integer>(); 
int anArray = locations.size(); 
int[] intArray = new int[anArray]; 
int i = 0; 
for(int location : locations){ 
    intArray[i++] = location; 
} 
+0

@ yegor256哎呀。我會假裝這是他作業的一部分.. *口哨聲* – AlanFoster 2012-02-22 00:36:27

0

假設你已經Integer對象的ArrayList,你可以簡單地通過ArrayList中環,並把元素intArray像這樣:

for (int index = 0; index < locations.size(); index++) { 
    intArray[index] = (Integer) locations.get(index); 
} 

希望有所幫助。

0

如何使用指定者():

ArrayList locations = new ArrayList(); 
int anArray = locations.size(); 
int[] intArray = new int[anArray]; 
intArray = locations.toArray(); 
相關問題