JavaSE-14.1.4【案例-Collection集合存储学生对象并遍历】
作者:互联网
package day5.lesson1; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /* 1.5 案例-Collection集合存储学生对象并遍历 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 */ public class CollectionDemo4 { public static void main(String[] args) { Collection<Student> collection = new ArrayList<>(); Student s1 = new Student("tom", 12); Student s2 = new Student("amy", 11); Student s3 = new Student("sam", 12); collection.add(s1); collection.add(s2); collection.add(s3); Iterator<Student> it = collection.iterator(); while (it.hasNext()){ Student stu = it.next(); System.out.println(stu.getName() + "," + stu.getAge()); } } }
package day5.lesson1; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
标签:14.1,name,age,Collection,collection,Student,JavaSE,public,String 来源: https://www.cnblogs.com/yppah/p/14826275.html