• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate a HashMap with 2 lists  RSS feed

 
Ranch Hand
Posts: 32
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have:






Now i have to populate my map, the Keys must be the names, like Fred, Martha.
And the Values must be the a list of Grades, but only the grades that have the same studentID as the name
So the result must be : {Fred =[10, 2], (because 10 and 2 have the same studentID as Fred
Martha = [4, 8], (because 4 and 8 have the same studentID as Martha) etc
My loop logic is wrong i tried everything, and i always end up with a map with all the grades: like {Fred =[10, 2, 4, 3]}
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Add the indicated println() call and see if that help you see what's going on. It's a very simple mistake, but it can be hard to spot if you don't take steps to actually observe what's happening in your program.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, you should never iterate a List with get(i). Use a foreach loop or explicit iterator instead.

Also, you're using s.size() for both end conditions, which is not right.

Here's a somewhat better way to write the loop (but still with the bug that's causing all grades to go to all students).


And note that the class name should be Grade, not Grades, since one instance of the class represents one Grade.

That loop could still be made a lot more efficient too, but let's not take on too much at once.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!