java arraylist 用法

我用Netbeans IDE 8.0.2 写java,部分程序如下:

ArrayList List = new ArrayList();

while(true){
if(input.nextInt() == 0) break;
List.add(input);
i++;

System.out.println("You have input "+ i + " numbers");
}

int[] num = new int[List.size()];

请问如何把List里面的元素放到新定义的num数组中?

如果输出的全是整型的话可以使用下面这种方式

ArrayList<Integer> List = new ArrayList<Integer>();
Integer[] num = new Integer[List.size()];
List.toArray(num);

如果输出的不确定那就得循环List一个一个给数组赋值了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-03
int len=List.size(); 
int[] array=new int[len];
for(int i=0;i<len;i++){
    array[i]=Integer.parseInt(List.get(i).toString()); 
}

第2个回答  2015-03-03
Integer[] num = (Integer[])List.toArray(new Integer[List.size()])

第3个回答  推荐于2016-09-13
最笨的方法是遍历集合,然后一个一个的放到数组中
for(int i=0;i<list.size();i++){
num[i]=list.get(i);

}

简单的方法是调用list的toArray方法
List list = new ArrayList();
list.add(1);
list.add(2);
int size =list.size();
Object[] arr = list.toArray();
System.out.println(Arrays.toString(arr));本回答被提问者采纳
第4个回答  2015-03-03
ava.util
Class ArrayList<E>
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

里面有一个toArray(), 可以试试看.

最不用想的做法, 就是用size, 一个个复制罗...