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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With