i don't see any get methods at all!!!
i guess you use the iterator() method.
i never learned using iterators. could someone show me how? here is the offending code(you might notice i am more used to using arrays):
i am just trying to get the sum of all the ints in the HashSet
A HashSet is a specific implementation of java.util.Set that is backed by a HashMap. Note that it doesn't implement java.util.Map (it "has a" Map, not "is a" Map). Strait from the javadoc, a Set "makes no guarantees as to the iteration order of the set", which is why trying to retrieve an object at a particular index will not work.
Is a Set truly what you are looking for here? Sets do not allow duplicates, so you would only get the sum of UNIQUE integers if you are using a Set.
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Joined: Oct 21, 2000
yes, no duplicates is why i chose it. i found an article that not only shows how to use iterators, but also the for-each statement. that should make it very easy.
if you ask me the java language is improving
i still have problems with this problem but the HashSet is not one now
With the new for-loop syntax you don't need to use an Iterator explicitly. You can also make use of auto(un)boxing, which means you don't need to call intValue() on the Integer object. You could write it like this: