Hey there,
I'm a bit stuck. I'm desperatly trying to find a way to remove from a HashSet, but everytime I do it, I get the error incompatible types- found java.lang.Objecs but expected Bag, while pointing at the line
Bag aCan = it.next();
I don't understand why it expects Bag- as far as I can see, on that line Bag is there. Any help would be great- I'm very much a beginner with this! (Which probably shows)
import java.util.HashSet; import java.util.Iterator; /** * Bag class */ public class Bag { private HashSet Bag = new HashSet<Can>(); /** * Constructor for objects of class Bag */ public Bag(int BagAmount) { HashSet<Can> Bag = new HashSet<Can>(); for (int i = 0; i < BagAmount; i++){ Bag.add(new Can()); } } /** * methods */ public int CanCount() { return Bag.size(); } public Can removeCan() { if (Bag.size() > 0); Iterator it = Bag.iterator(); while (it.hasNext()) { Bag aCan = it.next(); Bag.remove(); } } }