2010-09-18 86 views

回答

11

是的,在列表中使用toArray()

參見:

List a = new ArrayList(); 
    a.add("foo"); 
    a.add("bar"); 
    System.out.println(a); // prints [foo, bar] 

    Object[] myArray = a.toArray(); 
    for (int i = 0; i < myArray.length; i++) { 
    System.out.println(myArray[i]); // prints 'foo' and 'bar' 
    } 

網上看到的,ideone

2

Object [] array = myArrayList.toArray(new Object [myArrayList.size()]);?

+0

它們是不同的類型,所以你不能簡單地做Object [] ar = myArrayList; – jbindel 2010-09-18 02:06:23

+0

此代碼甚至沒有編譯... – Patrick 2012-02-06 07:48:15

+1

也許你想說對象[] array = myArrayList.toArray(new Object [myArrayList.size()]);? – Patrick 2012-02-06 07:53:01