我遇到了一些代碼的問題。我正在嘗試創建一個學生數據庫。我需要創建一組由學科名稱表示的學生。課程名稱映射到學生組。我試圖寫'add'方法,但是當我嘗試將它輸入到數據庫時,我收到一條錯誤消息:put(java.lang.String,java.util.Set)in java.util.Map> can not應用於(java.lang.Integer,StudentDatabase.Student)。任何幫助是極大的讚賞!!!!Java集合 - 映射和集合:put不能應用
import java.util。*;
公共類StudentDatabase {
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
}
我剛建的東西幾乎一模一樣,你在做什麼。添加一些評論,解釋你的設備存儲的內容以及你正在映射的內容,我可以提供幫助。這是一個學校項目嗎?如果是這樣,請發佈指向鏈接(如果可用)。 – Thorn 2013-04-09 00:36:58
該集合存儲學生身份證號碼。每門課程都以一組學生身份證號碼錶示。數據庫必須從課程名稱映射到id集。 – javanewbie 2013-04-09 00:42:51
我認爲這很簡單,我可以幫助你。但是我不明白你的add方法中idInt是什麼?請提供一些細節。 – 2013-04-09 04:47:06