package com.company;
public class Student {
private String name;
private int age;
private int score;
private String classNum;
public Student(String name,int age,int score,String classNum){
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
}
package com.company;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Students {
public static void main(String[] args) {
Map<String,List<Integer>> ClassNum2Score = new HashMap<>();
List<Student> StudentList = new ArrayList<>();
StudentList.add(new Student("Tom", 18, 100, "class05"));
StudentList.add(new Student("Jerry", 22, 70, "class04"));
StudentList.add(new Student("Owen", 25, 90, "class05"));
StudentList.add(new Student("Jim", 30,80 , "class05"));
StudentList.add(new Student("Steve", 28, 66, "class06"));
StudentList.add(new Student("Kevin", 24, 100, "class04"));
int total = 0;
for (Student student : StudentList){
total += student.getAge();
}
System.out.println("学生的平均年龄是:"+total/StudentList.size());
for (Student student:StudentList){
if (ClassNum2Score.containsKey(student.getClassNum())){
List<Integer> list = ClassNum2Score.get(student.getClassNum());
list.add(student.getScore());
ClassNum2Score.put(student.getClassNum(),list);
}
else {
List<Integer> ScoreList = new ArrayList<>();
ScoreList.add(student.getScore());
ClassNum2Score.put(student.getClassNum(),ScoreList);
}
}
int avgscore = 0;
Map<String,Integer> Class2Score = new HashMap<>();
for (String string :ClassNum2Score.keySet()){
List<Integer> list = ClassNum2Score.get(string);
int classscore = 0;
for (Integer integer : list){
classscore += integer;
}
avgscore = classscore/list.size();
Class2Score.put(string,avgscore);
}
for (String ss:Class2Score.keySet()){
System.out.println(ss+"的平均成绩是"+Class2Score.get(ss));
}
}
}
集合算平均年龄和平均成绩
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 一般来说,喜欢站立的人,比喜欢躺着或或坐着的人,身材更好。与躺着的姿势相比,站姿所消耗的能量要多出10%。而单腿站...
- 由北京初学文化传播有限公司(艺贰叁书画平台)主办的“千障神韵,意色苍莽 ” ——李汉德山水画展,将于9月10日在晋...