Is this Java code correct?
iterator, java, loops
Solution
Neither code is not "wrong", in the sense that both do what is expected. The second code, although equivalent, pollutes the local variables, because `iter` remains defined after the loop ends.
Problem
I am having a discussion (read argument!) with one of my colleagues. I maintain that this code is very wrong but he thinks there is nothing wrong with it: ``` for (Iterator<String> iter = collectionOfStrings.iterator(); iter.hasNext();) { String item = iter.next(); ... } ``` I maintain that this code is wrong because there is a duplication of looping. Either use Iterator or use a For loop but there is no need to use them both at the same time. I would re-write the code as follows: ``` Iterator<String> iter = collectionOfStrings.iterator(); while (iter.hasNext()) { String item = iter.next(); ... } ``` What do you think?