2012-02-22 7 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 –

答えて

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

0

あなたはIntegerオブジェクトのArrayListのは、あなたは単に、ArrayListのループスルーをすることができていると仮定し、そのようintArrayに要素を置く:助け

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

希望を。

0

方法のtoArray()の使用について:重複

ArrayList locations = new ArrayList(); 
int anArray = locations.size(); 
int[] intArray = new int[anArray]; 
intArray = locations.toArray(); 
関連する問題