我正在处理一个需要我将值添加到哈希图中的练习问题。但我不明白为什么我一直在线收到错误消息courseName.add(student);
这是我的代码:
public class StudentDatabase {
// add instance variables
private Map<String, HashSet<Integer>> dataContent = new LinkedHashMap<String, HashSet<Integer>>();
// Prints a report on the standard output, listing all the courses and all the
// students in each. If the map is completely empty (no courses), prints a message
// saying that instead of printing nothing.
public void report() {
if (dataContent.isEmpty()) {
System.out.println("Student database is empty.");
} else {
for (String key : dataContent.keySet()) {
System.out.println(key + ":" + "\n" + dataContent.get(key));
}
}
}
// Adds a student to a course. If the student is already in the course, no change
// If the course doesn't already exist, adds it to the database.
public void add(String courseName, Integer student) {
if (dataContent.containsKey(courseName)) {
courseName.add(student);
} else {
Set<Integer> ids = new HashSet<Integer>();
ids.add(student);
dataContent.put(courseName, ids);
}
}
}