案例:Collection集合存储学生对象并遍历

public class CollectionDemo {
    public static void main(String[] args) {
        //创建collection集合对象
        Collection<Student> c = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("旭旭宝宝",30);
        Student s2 = new Student("任怡旭",25);
        Student s3 = new Student("大马猴",22);

        //把学生添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);

        //遍历集合
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
        }

    }
}

 

posted @ 2020-04-06 19:28  硬盘红了  阅读(281)  评论(0编辑  收藏  举报