I'm currently working on a simple program to add students and quiz scores to a HashMap. I'm able to add a student and his/her corresponding quiz scores but when adding scores, they are all the same as I'm reading every keys value as the same ArrayList. I'd like to be able to add 3 scores for each student, rather than every student having the same increasing list of scores. Just looking for some direction regarding this as I can't find a ton of info regarding ArrayList in a Hashmap. Maybe I'm just looking in the wrong place.
UserIO inputReader = new UserIO();
HashMap<String, ArrayList> students = new HashMap<>();
ArrayList<Integer> quizScores = new ArrayList<>();
The UserIO class is just a simple input/output class. And here is my code for adding a student.
//add a student
String name = inputReader.readString("Please enter student name:");
quizScores.add(inputReader.readInt("Enter score for quiz 1: ", 0, 100));
quizScores.add(inputReader.readInt("Enter score for quiz 2: ", 0, 100));
quizScores.add(inputReader.readInt("Enter score for quiz 3: ", 0, 100));
students.put(name, quizScores);
break;
Will I need to instantiate a new ArrayList for each individual student? Would a loop around my add code be helpful if that were the case?
Here is part of the prompt from the assignment:
The program must store student quiz data in a HashMap that has the student name as the key and an ArrayList of integers as the values. (Will this affect how you calculate average scores?)
Edit: an example of what I'm trying to stop from happening, after adding one student I get:
Mike [45, 67, 78]
and two students:
Mike [45, 67, 78, 45, 87, 68] Jim [45, 67, 78, 45, 87, 68]