黑马程序员技术交流社区
标题:
.分析以下需求,并用代码实现: (1)定义一个学生类Student...
[打印本页]
作者:
yunmu
时间:
2016-4-25 22:20
标题:
.分析以下需求,并用代码实现: (1)定义一个学生类Student...
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
List<Student1> list = new ArrayList<>();
list.add(new Student1("张三","001",86));
list.add(new Student1("李四","001",88));
list.add(new Student1("王五","001",84));
list.add(new Student1("赵六","001",81));
list.add(new Student1("周七","001",93));
list.add(new Student1("李明","002",90));
list.add(new Student1("张伟","002",85));
list.add(new Student1("王华","002",79));
list.add(new Student1("刘红","002",94));
list.add(new Student1("吴青","002",80));
Object[] arr = list.toArray(); //将list集合转化为数组
String[] str1 = new String[list.size()];//存储姓名
String[] str2 = new String[list.size()];//存储编号
Integer[] integer = new Integer[list.size()];//存储
for(int i = 0;i < list.size();i++){
Student1 s = (Student1)arr[i];
str1[i] = s.getName();
str2[i] = s.getNo();
}
//创建双列集合,键是编号,值是具有相同编号的数量
Map<String,Integer> map1 = new HashMap<String,Integer>();
//存储编号,以及具有相同编号的数量
for(int i = 0;i < list.size();i++){
map1.put(str2[i],!map1.containsKey(str2[i]) ? 1 : map1.get(str2[i])+1);
}
String[] str3 = new String[map1.size()];//存储编号种类
for (String key : map1.keySet()) { //map.keySet()代表所有键的集合
int k = 0;
str3[k] = key; //将键值即编号种类
integer[k] = map1.get(key);//map.get(key)根据键获取值
k++;
}
for (int j = 0;j < str3.length;j++) {//遍历编号种类
double sum = 0;//记录总分
for(int i = 0;i < list.size();i++){//遍历编号找匹配编号
Student1 s1= (Student1)arr[i];
str1[i] = s1.getName();
str2[i] = s1.getNo();
if(str2[i].equals(str3[j])){//若编号种类匹配
sum = sum + s1.getScore();//从该编号获取分数相加
}
}
System.out.println(str3[j]+"班的总成绩是:"+sum+"平均成绩是:"+(sum/integer[j]));
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2