给定一个基本数据类型如下:
class Student{
int score;
String classId;
}
给定3个班级,班级id号分别为c1,c2,c3,随机生成一个List<Student> students; 该students的size()为15000,每一个学生Student的班级随机从c1、c2和c3中选取,每一个学生Student的成绩score随机从60到100随机选取,输出为每个班级里学生的平均成绩(多线程)。
参考代码
class Solution {
public static void solution(List<Student> list) {
List<Student> listC1 = new ArrayList();
List<Student> listC2 = new ArrayList();
List<Student> listC3 = new ArrayList();
for(int i = 0; i < list.size(); i++){
Student stu = list.get(i);
if("c1".equals(stu.classId)){
listC1.add(stu);
} else if("c2".equals(stu.classId)){
listC2.add(stu);
} else if("c3".equals(stu.classId)){
listC3.add(stu);
}
}
CountThread countThread1 = new CountThread(listC1);
countThread1.start();
CountThread countThread2 = new CountThread(listC2);
countThread2.start();
CountThread countThread3 = new CountThread(listC3);
countThread3.start();
}
}
class CountThread extends Thread {
List<Student> list;
int sum = 0;
int meanScore = 0;
public CountThread(List<Student> list){
this.list = list;
}
@Override
public void run(){
for(int i = 0;i < list.size();i++){
sum = sum + list.get(i).score;
}
meanScore = sum / list.size();
System.out.println("meanScore: " + meanScore);
}
}
class Student{
int score;
String classId;
}
enum ClassIdEnum {
C_1("c1",1),
C_2("c2",2),
C_3("c3",3);
private String code;
private int num;
ClassIdEnum(String code,int num) {
this.code = code;
this.num = num;
}
public static String getCodeByNum(int num) {
if(num == 1){
return C_1.code;
}else if(num == 2){
return C_2.code;
}else if(num == 3){
return C_3.code;
}
return null;
}
}
class ShowMeBug {
private static List<Student> studentRandom() {
Random random = new Random(1);
ArrayList<Student> list = new ArrayList();
for (int i = 0; i < 10; i++){
Student stu = new Student();
stu.score = random.nextInt(40) + 61;
System.out.println(stu.score);
int j = random.nextInt(3) + 1;
System.out.println(j);
String code = ClassIdEnum.getCodeByNum(j);
stu.classId = code;
list.add(stu);
}
return list;
}
public static void main(String[] args) {
System.out.println("Hello World!");
List<Student> list = studentRandom();
Solution.solution(list);
}
}
|