Initialize method from HashSet

79 views Asked by At

I have added the element spells.add(new Magic("boring",2,true)); and similar, but when I try to run

for (Magic set : spells) {
        Magic magic1 = new Magic(spells);
        magic1.go();
}

I get the error no suitable constructor found for Magic(java.util.HashSet<Magic>

If I change Magic magic1 = new Magic(spells); to Magic magic1 = new Magic("boring",2,true), I get the results I was looking for, but I want to be able to use a HashSet.

How can I make the contents of the HashSet usable?

1

There are 1 answers

0
Jon Skeet On BEST ANSWER

Why are you constructing a new Magic instance within the loop (or trying to, anyway)? Surely you just need the ones in the set:

for (Magic spell : spells) {
    spell.go();
}

Note that currently you're ignoring the set variable in your loop as well - which I've renamed to spell to be more obvious. Whenever you find yourself declaring a variable and then ignoring it, that's a hint that something's wrong...