首页 > 其他 > 详细

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

时间:2020-04-07 22:11:08      阅读:88      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

public class ListDemo {
    public static void main(String[] args) {
        //创建List集合对象
        List<Student> list = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("歪比歪比", 30);
        Student s2 = new Student("阿巴阿巴", 20);
        Student s3 = new Student("芜湖起飞", 66);

        //将学生对象添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);

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

        System.out.println("--------");

        //遍历集合,(for循环)
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}

运行结果:

技术分享图片

 

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

原文:https://www.cnblogs.com/pxy-1999/p/12656059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!