需求:使用Collection集合存储学生对象的信息
分析:
1.定义一个学生类(成员变量、无参带参构造方法、set\get方法)
2.定义测试类
3.在测试类中创建集合对象
4.创建学生对象并存储到集合中
5.使用迭代器遍历结合
代码演示:
/*测试类
* */
public class CollectionDemo {
    public static void main(String[] args) {
        //创建集合对象
        Collection<Student> s=new ArrayList<Student>();
        //创建学生对象并赋值
        Student s1=new Student("张三",14);
        Student s2=new Student("李四",15);
        Student s3=new Student("王五",16);
        s.add(s1);
        s.add(s2);
        s.add(s3);
        //创建迭代器
        Iterator<Student>it =s.iterator();
        //遍历集合并输出
        while(it.hasNext()){
            Student s4= it.next();
            System.out.println(s4.getName()+s4.getAge());
        }
    }
}
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号