我有一个List对象list,里面的对象是某类型的数据,如:SomeClass,以下是我的代码:SomeClass[] sc = null;
sc = (SomeClass[])list.toArray();结果sc并没有成功获得数据,而是在转换时抛出ClassCastException?重写代码:SomeClass[] sc = null;
sc = (SomeClass[])list.toArray(new SomeClass[list.size()]);结果没有问题,很奇怪,想请教List.toArray() 和 List.toArray(Object[] obj)有什么不同呢?补充一句,我的环境是 jdk1.4.2

解决方案 »

  1.   

    list.toArray返回的是Object [] 所以不能直接造型.
    除非你add的时候就是放的SomeClass [] get出来直接造型就可以
      

  2.   

    /**
         * Returns an array containing all of the elements in this list
         * in the correct order.
         *
         * @return an array containing all of the elements in this list
         *         in the correct order.
         */
        public Object[] toArray() {
    Object[] result = new Object[size];
    System.arraycopy(elementData, 0, result, 0, size);
    return result;
        }    /**
         * Returns an array containing all of the elements in this list in the
         * correct order; 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
         * <tt>null</tt> 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 a is not a supertype
         *         of the runtime type of every element in this list.
         */
        public Object[] toArray(Object a[]) {
            if (a.length < size)
                a = (Object[])java.lang.reflect.Array.newInstance(
                                    a.getClass().getComponentType(), size); System.arraycopy(elementData, 0, a, 0, size);        if (a.length > size)
                a[size] = null;        return a;
        }
      

  3.   

    不能这样整体转换就像下面的代码也会出现转型错误一样
          Object[] o= new Object[2];
          o[0]= "1";
          o[1]= "2";
          String[] s=(String[])o;
          System.out.println(s[0]);但是向上转型是可以的,这就涉及到一个协变的问题
    你仍然需要一项一项的转换类型
      

  4.   

    可以这么写
    SomeClass[] sc = null;
    list.toArray(sc);
    根本就不需要转化了!
      

  5.   

    我没事过,不知道会不会出错,如果出错的话数组可能需要初始化一下
    SomeClass[] sc = new SomeClass[list.size()];
    list.toArray(sc);
      

  6.   

    可以这么写
    SomeClass[] sc = null;
    list.toArray(sc);
    根本就不需要转化了!不错才怪!
      

  7.   

    数组和其他类型一样
    Object[] 是Object的子类型
    String[] 也是Object的子类型
    他们当然是不兼容的
      

  8.   

    这个绝对没有问题
    SomeClass[] sc = new SomeClass[list.size()];
    list.toArray(sc);
      

  9.   

    我是新学的新手,写过一些list的语句。第一个回答的人讲的很有道理。顺着他的思路
    你可以试试用 intstanceof 这个判断你的someclass 与Object是否匹配。如果不匹配,当然不能强制转换。这个判断好象很重要。if ( xxx instanceof someclass ) (someclass)*****
    这样就不会出cast的异常了
      

  10.   

    拷贝本身并不能解决强制转换的异常抛出。instanceof是个类型转换过程中很重要的函数。
      

  11.   

    有个笔误,不好意思。instanceof
      

  12.   

    后来我自己也通过细读Java Doc解决了问题,谢谢大家,就是用 Object[] List.toArray(Object[] obj) 这个方法轻松解决了问题。问题是,“The correct order”是按什么排序的呢?