首页 > 代码库 > 面向对象分拣存储
面向对象分拣存储
题目:一个Student类,属性name姓名,no编号,score成绩,现在将若干个student对象放入List,请统计出每个班级的总分和平均分
/** * 学生类 * @author student * */public class Student { private String name;//姓名 private String no;//编号 private double score;//成绩 public Student(String name, String no, double score) { super(); this.name = name; this.no = no; this.score = score; } public Student() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
/** * 一个班级多个学生(学生列表) * @author classroom * */public class ClassRoom { private String no;//班级编号 private List<Student> stuList;//班级列表 private double total;//总分 public ClassRoom() { stuList=new ArrayList<Student>(); } public ClassRoom(String no) { this(); this.no=no; } public ClassRoom(String no, List<Student> stuList, double total) { super(); this.no = no; this.stuList = stuList; this.total = total; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public List<Student> getStuList() { return stuList; } public void setStuList(List<Student> stuList) { this.stuList = stuList; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
/** * 一个Student类,属性name姓名,no编号,score成绩,现在将若干个student对象放入List,请统计出每个班级的总分和平均分 * 面向对象分拣存储 * @author MapDemo * */public class MapDemo { public static void main(String[] args) { //1.考试 List<Student> stuList=exam(); //2.分析成绩 Map<String ,ClassRoom> map=count(stuList); //查看总分 平均分 view(map); } /** * 查询每个班的总分和平均分 * 遍历map */ public static void view( Map<String ,ClassRoom> map){ Set<String> keys=map.keySet(); //获取迭代器对象 Iterator<String> keysIt=keys.iterator(); //先判断 while(keysIt.hasNext()){ //在获取 String no=keysIt.next(); ClassRoom room=map.get(no); //查看总分 计算平均分 double total=room.getTotal(); double avg=total/room.getStuList().size(); System.out.println(no+"总分:"+total+"平均分:"+avg); } } /** * 统计分析 * 1、面向对象 * 2、分拣存储 */ public static Map<String ,ClassRoom> count(List<Student> list){ Map<String ,ClassRoom> map=new HashMap<String,ClassRoom>(); //1.遍历List for (Student student : list) { //分拣查出是否存在该编号的班级 String no=student.getNo();//班级编号 double score=student.getScore();//学生成绩 //如果不存在就创建班级 ClassRoom room=map.get(no); if(null==room){ room=new ClassRoom(no); map.put(no, room); } //存在放入学生 room.getStuList().add(student);//放入学生 room.setTotal(room.getTotal()+score);//计算总分 } return map; } /** * 模拟考试,测试数据放到List中 */ public static List<Student> exam(){ List<Student> list=new ArrayList<Student>(); //存放学生的成绩 list.add(new Student("张三","1班",90)); list.add(new Student("李四","1班",80)); list.add(new Student("王五","1班",60)); list.add(new Student("赵六","2班",90)); list.add(new Student("王大锤","2班",80)); return list; }}
面向对象分拣存储
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。