import java.util.*;public class student{
public String name;
public int number;
public student(String n,int num){
name=n;
number=num;
}
public String toString(){
return this.name;
}

public static void main(String[] args){
List<student> sheet=new ArrayList<student>();
sheet.add(new student("haha",100));
sheet.add(new student("haha2",20));
sheet.add(new student("haha3",10));
for(student i:(student[])sheet.toArray()){//这里怎么错了?
System.out.println(i);
}
}
}Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lstudent;
at student.main(student.java:19)

解决方案 »

  1.   

    弄清这两个方法的区别:
    toArray
    public Object[] toArray()按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。 
    由于此列表不维护对返回数组的任何引用,,因而它将是“安全的”。(换句话说,此方法必须分配一个新的数组)。因此,调用者可以自由地修改返回的数组。 此方法担当基于数组的 API 和基于 collection 的 API 之间的桥梁。 toArray
    public <T> T[] toArray(T[] a)按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果指定的数组能容纳列表,则将该列表返回此处。否则,将分配一个具有指定数组的运行时类型和此列表大小的新数组。 
    如果指定的数组能容纳队列,并有剩余的空间(即数组的元素比队列多),那么会将数组中紧接 collection 尾部的元素设置为 null。(仅 在调用者知道列表中不包含任何 null 元素时才能用此方法确定列表长度)。 
    所以改完后的程序是;
    import java.util.*;public class student{
        public String name;
        public int number;
        public student(String n,int num){
            name=n;
            number=num;
        }
        public String toString()
        {
            return this.name;
        }
          public static void main(String[] args)
          {
            List<student> sheet=new ArrayList<student>();
            sheet.add(new student("haha",100));
            sheet.add(new student("haha2",20));
            sheet.add(new student("haha3",10));
            for(student i:(student[])sheet.toArray(new student[]{}))
    {
                System.out.println(i);
            }
        }
    }
      

  2.   

    强制转换的直接作用对象是Object类型的,通常只能支持普通的类型对象转化
    Student[]包含两层转换,即数组和Student类型
      

  3.   


    ArrayList 内部不存储具体的类型信息,除非你在调用 toArray() 的时候给它一个类型信息,否则它自己只会建立 Object[] 数组,Object[] 数组 强转为 student[] 当然是不行的。
      

  4.   

    sheet.toArray(new new student[size])
      

  5.   

    sheet.toArray(new student[sheet.size()])
      

  6.   


    ArrayList中关于toArray的源码:private transient Object[] elementData;
     
    private int size;public Object[] toArray() {
    return Arrays.copyOf(elementData, size);
    }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;
    }
      

  7.   


    import java.util.*;class Stu {
    public String name;
    public int number; public Stu(String n, int num) {
    name = n;
    number = num;
    } public String toString() {
    return this.name;
    }
    }public class TestArrayList {
    private static void testStu() {
    List<Stu> stuList = new ArrayList<Stu>();
    stuList.add(new Stu("ha", 1));
    stuList.add(new Stu("hb", 2));

    /*
     * 如果想使用Object[] toArray()强转,可以使用这种方式
     */
    for (Object obj : (Object[]) stuList.toArray()) {
    Stu stu = (Stu) obj;
    System.out.println(stu.name);
    System.out.println(stu.name);
    }
    }

    public static void main(String[] args) {
    testStu();
    }
    }
      

  8.   

    按照我个人理解,简单点说其实就是student[]与Object[]之间没有继承关系,所以不能强转。
      

  9.   

    弱弱的问一下,这里面的toString方法是怎么使用的?