创建collection对象后: Collection c = new ArrayList();
采用方法 Object toArray()的方法,返回是一个Object型的集合中所有元素的值。
注意问题:
我定义了一个集合
Collection c = new ArrayList();
c.add(new Student("小明",20));
c.add(new Student("小明",23));
c.add(new Student("小黄",22));
c.add(new Student("小黑",21));
Object[] arr = c.toArray();
//Student[] s = (Student[])arr; 为什么不能这么转型???
for (int i = 0; i < arr.length; i++) {
Student s = (Student)arr[i]; //此时要想用Student类中的方法,应该要使Object类型的
//数组进行向下转型,变为Student类型才可以继续调用
//Student类中的方法
System.out.print(s.getName() + " ");
}