public class StudentDome {
public static void main(String[] args) {
ArrayList<Student> c=new ArrayList<>();
Student s1 = new Student("张三",19);
Student s2 = new Student("李四",20);
c.add(s1);
c.add(s2);
Iterator<Student> sc=c.iterator();
while (sc.hasNext()){
Student i=sc.next();
System.out.println(i.getName()+i.getAge());
}
}
}
与
public class StudentDome {
public static void main(String[] args) {
Collection<Student> c=new ArrayList<>();
Student s1 = new Student("张三",19);
Student s2 = new Student("李四",20);
c.add(s1);
c.add(s2);
Iterator<Student> sc=c.iterator();
while (sc.hasNext()){
Student i=sc.next();
System.out.println(i.getName()+i.getAge());
}
}
}
输出不是完全一样的吗
两者有什么区别吗 用途不一样吗
Collection是接口,ArrayList是实体类
接口不能自己实例化,实体类可以实例化,类的实例化对象可以用实现的接口接收
ArrayList a = new ArrayList() 可以调用ArrayList所有的方法
Collection a = new ArrayList() 只能调用Collection中有的方法