import java.util.*;
public class TestList1{
public static void main(String[] args) {
List<String> allList = new ArrayList<String>();  
allList.add("Hello");  
allList.add(0,"World");
allList.add("Hello");  
allList.add("World");
        String str[] = allList.toArray(new String[]{});
            //上面toArray();里的参数,new出来的数组没有指明大小,而且后面还有个大括号,这么写不太明白啊
for (int i = 0; i < str.length; i++) { 
    System.out.print(str[i] + "、");
}
}    
}

解决方案 »

  1.   

    jdk的ArrayList的:    /**
         * Returns an array containing all of the elements in this list in proper
         * sequence (from first to last element); the runtime type of the returned
         * array is that of the specified array.  If the list fits in the
         * specified array, it is returned therein.  Otherwise, a new array is
         * allocated with the runtime type of the specified array and the size of
         * this list.
         *
         * <p>If the list fits in the specified array with room to spare
         * (i.e., the array has more elements than the list), the element in
         * the array immediately following the end of the collection is set to
         * <tt>null</tt>.  (This is useful in determining the length of the
         * list <i>only</i> if the caller knows that the list does not contain
         * any null elements.)
         *
         * @param a the array into which the elements of the list are to
         *          be stored, if it is big enough; otherwise, a new array of the
         *          same runtime type is allocated for this purpose.
         * @return an array containing the elements of the list
         * @throws ArrayStoreException if the runtime type of the specified array
         *         is not a supertype of the runtime type of every element in
         *         this list
         * @throws NullPointerException if the specified array is null
         */
        public <T> T[] toArray(T[] a) {
            if (a.length < size)
                // Make a new array of a's runtime type, but my contents:
                return (T[]) Arrays.copyOf(elementData, size, a.getClass());
    System.arraycopy(elementData, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }
      

  2.   


                    //试下你就知道了
    String[] qq =new String[]{};
    System.out.println(qq.length);

    String[] qq =new String[]{"67","ddd"};
    System.out.println(qq.length);
      

  3.   

    String str[] = allList.toArray(new String[]{});
                //上面toArray();里的参数,new出来的数组没有指明大小,而且后面还有个大括号,这么写不太明白啊
    这么写是指定数组的类型,看下jdk的API吧
      

  4.   

    好像在jdk1.5开始没必要指明了吧。因为用到泛型了
      

  5.   

    String[] str = (String[])allList.toArray(new String[allList.size()]); 
      

  6.   

    new String[]{}  等同于  new String[0];allList.toArray(new String[]{}) ,传入的这个数组是告诉List把数据填到这个数组,  但是List如果发现这个数组太小的话,  它会重新new一个跟传入数组同类型的数组来存数据
      

  7.   


    这个才是正解!
    还要注意,如果指定的数组的长度大于list的size。则后面剩余的部分全部为null。
      

  8.   

    用 new String[0] 或者 new String[list.size()] 都可以
      

  9.   

    1.还是有必要呢,因为不指明默认是转换为Object的
    2.#1的源代码给的很好,学习ing
      

  10.   

    jdk1.5中引入的泛型,不在需要强制转换了
    你可以使用下面的代码来看看,下面的是不能转化的
    import java.util.*;
    public class Test{
        public static void main(String[] args) {
            List<Object> allList = new ArrayList<Object>();  
            allList.add("Hello");  
            allList.add(0,"World");
            allList.add("Hello");  
            allList.add("World");
            String str[] = (String[])allList.toArray();
                     for (int i = 0; i < str.length; i++) { 
                System.out.print(str[i] + "、");
            }
        }    
    }
      

  11.   

    public <T> T[] toArray(T[] a)
    a - 要在其中存储列表元素的数组(如果它足够大);否则,为此分配一个具有相同运行时类型的新数组。 
      

  12.   

    他这样是先new一个string数组出来,再把arraylist集合的对象放到string数组里,string数组会跟据集合的大小变化。大概是这样