大家帮我看看那里错了给我解释解释
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import com.heima.bean.Student;
public class Demo_collection {
/**
* @param args
*/
public static void main(String[] args) {
Demo();
//demo2();
}
private static void demo2() {
Collection c=new ArrayList();
c.add(new Student("张三",23));
c.add(new Student("李四",24));
c.add(new Student("王五",25));
Iterator it=c.iterator();//获取迭代器 返回的是一个Iterator
while(it.hasNext()){//判断该集合中是否有元素
Object obj=it.next();
Student s=(Student)obj;
//Student s=(Student)it.next();
System.out.println(s.getName()+"..."+s.getAge());
}
}
private static void Demo() {
Collection c=new ArrayList();
c.add(123);
c.add(new Student("张三",23));
c.add(new Student("李四",24));
c.add(new Student("王五",25));
c.add(new Student("赵六",26));
Object[] arr=c.toArray();//不知道集合中是什么类型就用object接收
for (int i = 0; i < arr.length; i++) {
//System.out.println(arr[i]);
//Student s=(Student)arr[i];
//System.out.println(s);
if(arr[i] instanceof Integer){
Integer s1=(Integer)arr[i];
System.out.println(s1);
}if(arr[i] instanceof Student){
Student s2=(Student)arr[i];
System.out.println(s2);
}
}
}
}
|
|