问题描述:
在实际项目开发中需要存储各种类型的对象数据,一般使用数组存储存在存储个数不确定问题,通常使用集合类型来存储。给你的问题是,在学生信息系统中一个院系有班级信息和学生信息,一个班级有多个学生,每个学生属于一个班,在信息查询中往往需要进行某学生在那个班,该同学有那些同班同学等相关查询,如果使用集合合理存储了班级和学生对象,进行相关查询将变得容易。
本问题使用标准输入班级信息和学生信息,然后查出给定某同学的班级,以及该班同学人数和学号最前、最后的学生学号。
输入:
标准输入,输入的第一行为两个正整数N、M,N表示接下来的N行为班级信息,每行由班级编号、班级名称、入学年级,数据之间为一个逗号","分格;班级信息后M行为学生信息,可能有重复学生信息,学生信息的每行依次为学号、所在班级编号、姓名、性别、入学年构成,数据之间为一个逗号","分格。学生数据之后的所有行为学生学号数据,需要你完成查找的学生,每个学生查找信息使用一行输出。
输出:
标准输出,根据需要你查找的学生的学号使用一行输出该学生所在的班级编号和该班学生个数,并输出该班学号排序的第一个和最后一个学生的学号,中间使用一个空格隔开。如果该班只有一个学生,输出第一个学生学号,如果该该学号没有学生,输出”NO Student“。
输入样列:
5 13
2006405,5,2020
2006408,8,2020
2106402,2,2021
2106405,5,2021
2106406,6,2021
2000130841,2006408,杨凯,男,2020
2000130842,2006408,杨石磊,男,2020
2000130849,2006408,刘坤艳,男,2020
2000130501,2006405,文吉鑫,男,2020
2000130502,2006405,方大卫,男,2020
2000130503,2006405,伍一帆,男,2020
2000130504,2006405,赵天祺,男,2020
2000130505,2006405,易佳豪,男,2020
2000130506,2006405,欧阳哲浩,男,2020
2000130510,2006405,陈吉涛,男,2020
2000130501,2006405,文吉鑫,男,2020
2000130511,2006405,曹毅凡,男,2020
2000130501,2006405,文吉鑫,男,2020
2000130506
2000130508
2000130842
输出样列:
2006405 8 2000130501 2000130511
NO Student
2006408 3 2000130841 2000130849
代码展示
import java.util.*;
public class Text8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//得到读入班级数据和学生数据的行数
int n = sc.nextInt();
int m = sc.nextInt();
//创建班级集合
HashMap<String, Class> classMap = new HashMap<String , Class>();
//创建学生集合
HashMap<String, student> studentMap = new HashMap<String, student>();
//读掉回车键
String line = sc.nextLine();
//读入班级数据
for(int i = 0; i < n; i ++){
line = sc.nextLine();
String[] str = line.split(",");
classMap.put(str[0],new Class(str[0]));
}
//读入学生数据
while(m -- > 0){
line = sc.nextLine();
String [] str = line.split(",");
student stu = new student(str[1],str[0]);
//判断学生是否重复出现
if(studentMap.get(stu.id) == null){
studentMap.put(stu.id,stu);
String myClass = stu.classId;
Class Class = classMap.get(myClass);
Class.stuList.add(stu);
}
}
//根据学生学号查询信息
while(sc.hasNextLine()){
line = sc.nextLine();
//判断有无此学生
if(studentMap.get(line) == null){
//无此学生
System.out.println("NO Student");
}else{
student stu = studentMap.get(line);
Class myClass = classMap.get(stu.classId);
//判断班级人数
if(myClass.stuList.size() == 1){
//班级人数为一
System.out.println(myClass.classId + " " +"1 " +stu.id);
}else {
//将学生集合转换为学生数组
student [] stus = new student[myClass.stuList.size()];
myClass.stuList.toArray(stus);
//将学生数据进行排序
Arrays.sort(stus,new MyCompareto());
//将数据输出
System.out.println(myClass.classId + " " + stus.length +
" " + stus[0].id + " " + stus[stus.length-1].id);
}
}
}
}
}
//简化数据,将不需要的数据剔除掉
class Class{
String classId ;
HashSet<student> stuList = new HashSet<student>();
Class(String classId){
this.classId = classId;
}
}
class student{
Class myClass;
String classId;
String id;
student(String classId, String id){
this.classId = classId;
this.id = id;
}
}
//定义比较的规则
class MyCompareto implements Comparator<student> {
@Override
public int compare(student o1, student o2) {
int start = Integer.parseInt(o1.id);
int end = Integer.parseInt(o2.id);
return start - end;
}
}
注意点我就不写了,因为感觉这种题目自己多写几遍,多错几次就会了,而且详细的解析我也写在代码中了,认真看,你一定能看懂的。相信自己,加油吧!!!!!