我有一个 Hashmap,它将学生姓名存储为键,将分数的 int 数组存储为值。我知道它正确创建了 HashMap,但是当尝试返回键的 int 数组时,我似乎无法得到。
public int[] getQuizzes(String studentName)
{
int[] studentsQuizzes = quizMarks.get(studentName);
return studentsQuizzes;
}
它最终只是返回 null。我缺少什么,感谢您的帮助
这就是我创建 HashMap 的方式
quizMarks = new HashMap<String, int[]>();
public void addStudent(String studentName)
{
String formattedName = formatName(studentName);
int[] quizzes = new int[NUM_QUIZZES];
for (int i = 0; i < quizzes.length; i++)
{
quizzes[i] = MIN_GRADE;
}
quizMarks.put(formattedName, quizzes);
}
最佳答案
您在 map 中的键是对传入的学生姓名调用 formatName
的结果。在调用 get
时,您似乎没有使用格式化的姓名作为键code> 在 map 上,这意味着您传递给 get
的键与传递给 put
的键不同。
关于java - 以 int 数组 (int[]) 作为值的 HashMap 在获取时返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542979/