0

我遇到了一些代码问题。我正在尝试创建一个学生数据库。我需要创建一组由课程名称表示的学生。课程名称映射到学生集。我尝试编写“添加”方法,但是当我尝试将其放入数据库时​​,我收到一条错误消息: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

}

4

2 回答 2

0

错误消息中的答案是正确的:put(java.lang.String,java.util.Set) in java.util.Map> cannot be applied to (java.lang.Integer,StudentDatabase.Student). 第二个列表中的哪个类型与第一个列表中的类型不匹配?

于 2013-04-09T00:32:28.173 回答
0

您需要将参数类型Map#put与您的声明相匹配Map<String, Set<Integer>>

Student info = new Student(idInt);
Set<Integer> searchMap = database.get(courseName);
if (searchMap == null) {
   searchMap = new HashSet<String>();
}

searchMap.add(new Integer(idInt));
database.put(courseName, searchMap);
于 2013-04-09T00:40:22.497 回答