2013-04-09 165 views
0

我遇到了一些代碼的問題。我正在嘗試創建一個學生數據庫。我需要創建一組由學科名稱表示的學生。課程名稱映射到學生組。我試圖寫'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 

}

+0

我剛建的東西幾乎一模一樣,你在做什麼。添加一些評論,解釋你的設備存儲的內容以及你正在映射的內容,我可以提供幫助。這是一個學校項目嗎?如果是這樣,請發佈指向鏈接(如果可用)。 – Thorn 2013-04-09 00:36:58

+0

該集合存儲學生身份證號碼。每門課程都以一組學生身份證號碼錶示。數據庫必須從課程名稱映射到id集。 – javanewbie 2013-04-09 00:42:51

+0

我認爲這很簡單,我可以幫助你。但是我不明白你的add方法中idInt是什麼?請提供一些細節。 – 2013-04-09 04:47:06

回答

0

的答案是正確的錯誤消息:put(java.lang.String,java.util.Set) in java.util.Map> cannot be applied to (java.lang.Integer,StudentDatabase.Student)。第二個列表中的哪個類型與第一個列表中的類型不匹配?

+0

對不起,我不明白...如果你可以詳細說明,將不勝感激.. – javanewbie 2013-04-09 00:41:22

0

你需要與你的宣言Map<String, Set<Integer>>匹配參數類型的Map#put

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); 
+0

即時得到非法類型的開始:searchMap = new HashSet <>(); – javanewbie 2013-04-09 00:50:57

+0

您使用的是Java 7嗎? – Reimeus 2013-04-09 00:51:34

+0

hmm nope我認爲1.6 – javanewbie 2013-04-09 00:52:41