Reputation: 13
I am trying to populate a HashSet in the constructor with Penny objects but I'm not really sure how to do this. I have written this but I keep getting error messages.
public Pocket(int numOfPennies){
HashSet penniesSet = new HashSet<Penny>();
while( penniesSet.size() <= numOfPennies){
penniesSet.add(Penny);
}
Upvotes: 0
Views: 4168
Reputation: 285415
You're not adding an object to the set but rather are trying to add a type, and this won't work or even compile. Instead of
penniesSet.add(Penny);
try
// assuming Penny has a default constructor
penniesSet.add(new Penny());
Also,
pennyA.equals(pennyB)
. Of course this would depend on how you define equals(...)
and hashCode()
for your Penny class.Upvotes: 5