2012-02-22 83 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(); 
相关问题