以 int 数组 (int[]) 作为值的 HashMap 在获取时返回 null?
我有一个 Hashmap,它将学生姓名存储为键,将分数的 int 数组存储为值。我知道它正确创建了 HashMap,但是当尝试返回键的 int 数组时,我似乎无法得到。
public int[] getQuizzes(String studentName)
{
int[] studentsQuizzes = quizMarks.get(studentName);
return studentsQuizzes;
}
它最终只是返回 null。我缺少什么,感谢您的帮助
这就是我创建哈希图的方式
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);
}
I have a Hashmap that stores a student name as the key and an int array of scores as the value. I know its creating the HashMap correctly but when trying to return the int array for a key I cant seem to get.
public int[] getQuizzes(String studentName)
{
int[] studentsQuizzes = quizMarks.get(studentName);
return studentsQuizzes;
}
It just ends up returning null. What am I missing, thanks for any help
This is how I am creating the 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
地图中的键是对传入的学生姓名调用
formatName
的结果。在调用get
时,您似乎没有使用格式化的名称作为键映射,这意味着您传递给get
的键与传递给put
的键不同。Your keys in the map are the results of calling
formatName
on the student name passed in. You don't appear to be using the formatted name as the key when callingget
on the map, meaning the keys you pass toget
are not the same as those you passed toput
.