import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
-
Iterator迭代器:遍历集合,获取该集合的迭代器完成迭代操作。
-
格式:Iterator<> 变量名=集合.iterator();
-
Iteratro的常用方法:
-
hasNext()方法:若集合仍有元素可以迭代,返回true.。
-
next()方法:返回迭代的下一个元素。
-
*/
public class HomeWorkDay02_05 {public static void main(String[] args) {
Student stu01=new Student("张三",20,82); Student stu02=new Student("李四",21,89); Student stu03=new Student("王五",22,90); Student stu04=new Student("赵六",23,91); Student stu05=new Student("钱七",24,99); Collection<Student> coll=new ArrayList<>(); coll.add(stu01); coll.add(stu02); coll.add(stu03); coll.add(stu04); coll.add(stu05); Iterator<Student> it=coll.iterator(); //定义总分 double totallScore=0; //定义最高分 double maxScore=((ArrayList<Student>) coll).get(0).getScore(); double mimScore=((ArrayList<Student>) coll).get(0).getScore(); while(it.hasNext()) { Student stu=it.next(); totallScore+=stu.getScore(); if(maxScore<stu.getScore()) maxScore=stu.getScore(); else if(mimScore>stu.getScore()) mimScore=stu.getScore(); } double avgScore=totallScore/coll.size(); System.out.println("总分:"+totallScore+"最高分:"+maxScore+"最低分:"+mimScore);
}
}