我遇到了一些代码问题。我正在尝试创建一个学生数据库。我需要创建一组由课程名称表示的学生。课程名称映射到学生集。我尝试编写“添加”方法,但是当我尝试将其放入数据库时,我收到一条错误消息:java.util.Map> 中的 put(java.lang.String,java.util.Set) cannot应用于 (java.lang.Integer,StudentDatabase.Student)。任何帮助是极大的赞赏!!!!
导入 java.util.*;
公共类学生数据库{
private Map<String, Set<Integer>> database = new TreeMap<String, Set<Integer>>();
private static class Student extends TreeSet<Integer> {
public int id;
public Student(int id){
this.id = id;
}
}
public void add(String courseName, Integer student) {
/* I've tried to use this way to add to the database and it doesn't work too.
Set<Integer> studentSet = database.get(courseName);
if (studentSet == null){
studentSet = new TreeSet<Integer>();
}
studentSet.add(student);
database.put(courseName, student);
*/
Integer idInt = new Integer(idInt);
if (database.containsKey(idInt)){
//if the student is a duplicate, that is ok
}
else{
Student info = new Student(idInt);
database.put(new Integer(idInt), info);
}
} // end add
}