题目 又如下Student对象
其中,classNum 表示学生的班级号,例如"class05"
有如下List:
List<Student> list = new ArrayList<Student>();
list.add(new Student("Tom",18,70,"class05"));
list.add(new Student("Jerry",22,70,"class04"));
list.add(new Student("Owen",25,70,"class05"));
list.add(new Student("Jim",30,70,"class05"));
list.add(new Student("Steve",28,66,"class06"));
list.add(new Student("Kevin",24,100,"class04"));
在这个list的基础上,完成下列要求:
i.计算所有学生的平均年龄
ii.计算各个班级的平均分
方法一 :思想是利用List集合下的ArrayList实现用两个ArrayList顶级List<List<Student>>与List<Student>,List<Student>内部存取具体的同一个班的stu对象。
Student类
public class Student {
private String name; //名字
private int age; //年龄
private double score; // 分数
private String classNum;// 学生的班号 "class05"
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public Student(String name, int age, double score, String classNum) {
super();
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
@Override
public String toString() {
return "Student [ classNum=" + classNum + "]";
}
}
public class TestStudent {
public static void main(String[] args) {
// 数据初始化添加
List<Student> list = new ArrayList<Student>();
list.add(new Student("Tom",18,70,"class05"));
list.add(new Student("Jerry",22,70,"class04"));
list.add(new Student("Owen",25,70,"class05"));
list.add(new Student("Jim",30,70,"class05"));
list.add(new Student("Steve",28,66,"class06"));
list.add(new Student("Kevin",24,100,"class04"));
// 1.计算所有学生的平均年龄
int count = 0;//记录学生人数
double age = 0; // 记录学生总年龄
for (Student student : list) {
age += student.getAge();
}
count = list.size();
System.out.println("学生平均年龄为:" + age/count);
// 2.计算各个班级的平均分
// 存储分好班级的列表
List<List<Student>> list2 = new ArrayList<List<Student>>();
// 标志位
boolean flag = false;
// 遍历所有的学生
for (int i = 0; i < list.size(); i++) {
flag = false;
// 存储每个班的学生
List<Student> list1 = new ArrayList<Student>();
// 判断集合 list2
if(list2.isEmpty()) {
list1.add(list.get(i));
list2.add(list1);
continue;
}
// 如果不是第一个
for (int j = 0; j < list2.size(); j++) {
// 发现重复
if (list.get(i).getClassNum().equals(list2.get(j).get(0).getClassNum())) {
// 逻辑
flag = true;
list2.get(j).add(list.get(i));
break;
}
}
//list list1每个班的学生 list2存储分好班级的列表
if(!flag) {
//否则是新的班级则添加进去
List<Student> l = new ArrayList<Student>();
l.add(list.get(i));
list2.add(l);
}
}
for (List<Student> students:
list2) {
double sumScore = 0.0;
for (Student stu : students){
sumScore += stu.getScore();
}
System.out.println(students.get(0).getClassNum() + "班级平均分为:" + sumScore/students.size());
}
}
}
方法二:[推荐]:使用Map下的HashMap键值存取利用Map<String,List<Student>>存取更加方便
import java.util.*;
import java.util.Map.Entry;
public class Student {
private String name; //名字
private int age; //年龄
private double score; // 分数
private String classNum;// 学生的班号 "class05"
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public Student(String name, int age, double score, String classNum) {
super();
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
public Student() {}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("Tom",18,70,"class05"));
list.add(new Student("Jerry",22,70,"class04"));
list.add(new Student("Owen",25,70,"class05"));
list.add(new Student("Jim",30,70,"class05"));
list.add(new Student("Steve",28,66,"class06"));
list.add(new Student("Kevin",24,100,"class04"));
// 1.计算所有学生的平均年龄
System.out.println("平均年龄为:" + average(list));
// 2.计算各个班级的平均分
Student student = new Student();
Map<String,Double> map = student.averscore(list);
for (Map.Entry<String,Double> mp1: map.entrySet()) {
System.out.println(mp1.getKey() + ":" +mp1.getValue());
}
}
// 1.计算所有学生的平均年龄
public static double average(List<Student> list) {
int age = 0;
double avera = 0.0;
for (Student student : list) {
age += student.getAge();
}
avera = (double)age / list.size();
return avera;
}
// 2.计算各个班级的平均分
public Map<String,Double> averscore(List<Student> list) {
// 1.需要一个Map集合记录并且分班
List<Student> list2 = new ArrayList<Student>();
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
// 2.遍历list集合
for (int i = 0; i < list.size(); i++) {
String keyString = list.get(i).getClassNum();// 取出class--当作Map集合的key
if(!map.containsKey(keyString)) {
List<Student> students = new ArrayList<Student>() ;
students.add(list.get(i));
map.put(keyString, students);
} else {
for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
if(entry.getKey() == keyString) {
List<Student> list3 = entry.getValue();
list3.add(list.get(i));
entry.setValue(list3);
break;
}
}
}
}
//以下是为了方便输出成绩计算的,也可以在使用时再计算
Map<String, Double> map2 = new HashMap<String, Double>();
for (Map.Entry<String, List<Student>> map1 : map.entrySet()) {
double sum = 0;
int num = 0;
for (int i = 0; i < map1.getValue().size(); i++) {
List<Student> aStudents = map1.getValue();
sum += aStudents.get(i).getScore();
num ++;
}
sum /= num;
map2.put(map1.getKey(), sum);
}
return map2;
}
}