Saturday, November 17, 2007

Spot the mistake

Can you spot the mistake in this code?

// BROKEN - throws NoSuchElementException!

for (Iterator i = suits.iterator(); i.hasNext(); )
for (Iterator j = ranks.iterator(); j.hasNext(); )
sortedDeck.add(new Card(i.next(), j.next()));

If not, check this article on the Java for...each loop. 



Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

No comments: