1

I'm trying to use a hashmap to store an int array of 4 integers. when I add to the hashmap it is supposed to add the most recent set of 4 integers and then generate a new set of 4 numbers. But instead it overwrites all values with the newly generated set of 4 integers... This seems weird since I never tell it to do so. Any ideas? Here is the code where it gets added

System.out.println("Games Played: " + gamesPlayed);
System.out.println("Size Before: " + scoreAnswer.size());
scoreAnswer.put(gamesPlayed, answer); //gamesPlayed will increase by 1 every time right after
System.out.println("Size After: " + scoreAnswer.size());
for (int i = 0; i < maxPin; i++) {
    System.out.println(answer[i]);
}
System.out.println("hash");
for (int i = 0; i < scoreAnswer.size(); i++) {
    int[] a = scoreAnswer.get(i);
    for (int j = 0; j < a.length; j++) {
        System.out.println("[" + i + "]"  + a[j]);
    }
}
gamesPlayed++;
System.out.println("Games Played: " + gamesPlayed);

when I run the program and have it add to the hashmap this is what will print out:

Games Played: 0, Size Before: 0, Size After: 1, 4 0 0 4, hash [0]4 [0]0 [0]0 [0]4 Games Played: 1

Everything is working here, untill i have it add another to the hash, it will then return this:

Games Played: 1, Size Before: 1, Size After: 2, 4 2 0 4, hash [0]4 [0]2 [0]0 [0]4 [1]4 [1]2 [1]0 [1]4, Games Played: 2

As you can see, the first set of integers have been overwritten to the 2nd set of integers. I have no idea why this is happening. Any help is appreciated!

EDIT: So sorry, my computer posted the question before I was finished typing all of my code.

4
  • 4
    What do you mean by "all the values"? It doesn't help that we don't know any of the types involved. A short but complete program demonstrating the problem would make it easier to help you. Commented Jan 6, 2015 at 20:15
  • 1
    We need an MCVE and a better problem statement to answer this. Your current description sounds vaguely to me like this has nothing to do with the Map but rather a misunderstanding of how array references work. Commented Jan 6, 2015 at 20:18
  • 1
    Do you ever re-initialize answer? Commented Jan 6, 2015 at 20:26
  • It looks like you should be using a List rather than a Map as the keys appear to be 0, 1, 2, ... . You still haven't given us a MCVE. We need a complete example including the declaration of scoreAnswer. Commented Jan 6, 2015 at 20:27

1 Answer 1

3

Based on your code, the variable "answer" must be the array you want to put into the hashmap, I guess you need to initialize it every time using the new keyword, like this

int[] answer = new int[4];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that was what I was missing, everything is working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.