[List和HashMap—练习]——统计学生成绩,并打印

本文介绍了一个使用Java集合框架的实际案例,包括如何创建Student类来存储学生的姓名、班号和成绩等信息。通过List和HashMap实现了对学生数据的组织、统计及显示,展示了如何计算每个班级的总分和平均分。

List和HashMap——练习

             定义一个Studeng类,属性姓名name,班号no,成绩score.将若干个Student对象放入List,请统计每个班级的总分和平均分,分别打印出来。
    按现实逻辑是这样的,每个Student对象拥有自己的姓名Name,班号no,成绩score属性。ClassRoom对象包含了班号no,总成绩total,还有一个List<Studeng>stus属性用来初始化一个班级。
public class Student {
	private String name;
	private String no;//班号
	private double score;
	public Student(){
	}
	public Student(String name, String no, double score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}
}//属性name,no,score的get和set方法
public class ClassRoom {
	private String no;
	private List<Student> stus;//学生列表
	private double total;//总分
	public ClassRoom(){
		stus=new ArrayList<Student>();
	}                                
	public ClassRoom(String no) {
		this();
		this.no = no;
	}
}<pre name="code" class="java">//属性stus,no,total<span style="font-family: Arial, Helvetica, sans-serif;">的get和set方法</span>

   main方法里集成了几个方法。

public static void main(String[] args) {
	List<Student> list =new ArrayList<Student>();//List<Student>对象用来存放所有的Student对象
	exam(list);//把学生的信息都放进了list里面
	Map<String, ClassRoom> rooms=new HashMap<String,ClassRoom>();
	count(rooms,list);//统计
	printScore(rooms);//打印
}
         exam(List<Student> list)函数
public static void exam(List<Student> list){//将若干个Student对象加入List
	list.add(new Student("a","001",89));
	list.add(new Student("b","002",79));
	list.add(new Student("c","003",85));
	list.add(new Student("d","004",79));
	list.add(new Student("e","005",99));
}
      rooms对象包含了Key(班名)和Value(ClassRoom对象)。就好比一个班名对应一间教室(ClassRoom),用于存放学生列表。
    count(Map<String, ClassRoom> rooms,List<Student>list)函数将rooms对象和学生列表放在了一起,以便操作两者数据。
public static void count(Map<String, ClassRoom> rooms,List<Student>list){
	for(Student stu:list){
		String no=stu.getNo();
		double score=stu.getScore();
		//根据班级编号,查看Map是否存在该班级,分拣思路
		ClassRoom room=rooms.get(no);
		if (null==room) {
			room=new ClassRoom(no);
			rooms.put(no,room);
		}
		//存储
		room.setTotal(room.getTotal()+score);
		room.getStus().add(stu);//加入学生
	}
}
          现在,各个学生已经加入了各自对应的班级,开始上课了。通过迭代器打印出Set里面的对象。
public static void printScore(Map<String, ClassRoom> rooms){
	Set<Map.Entry<String, ClassRoom>> entrySet=rooms.entrySet();
	Iterator<Map.Entry<String, ClassRoom>> it=entrySet.iterator();
	while (it.hasNext()) {
		Map.Entry<String, ClassRoom> entry=it.next();
		ClassRoom room=entry.getValue();//返回与此项对应的值
		double avg=room.getTotal()/room.getStus().size();
		System.out.println("班号为: "+room.getNo()+"总分"+room.getTotal()+"平均分"+avg);
		
	}
}
         这里的迭代器遍历Set很重要,需好好体会。
   另外printScore(Map<String, ClassRoom> rooms)函数还可以用面向对象的思想这样写:

public static void printScore(Map<String, ClassRoom> rooms){
	Set<String> keys=rooms.keySet();
	for(String key:keys){
		ClassRoom room=rooms.get(key);
		double avg=room.getTotal()/room.getStus().size();
		System.out.println("班号为: "+room.getNo()+"总分"+room.getTotal()+"平均分"+avg);
	}
}
       后者较为简单,但两者的思想都很重要。

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值