I don’t delude myself by calling myself a Java guru, let alone an expert, but occasionally I am reminded of something that just makes me really realise how much of a noob I can be. Take for example the For-Each construct. For as long as I can remember I’ve been using the following pattern for creating a simple loop through a collection.
for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )
Today however my IDE handily popped up a hint asking if I wanted convert my simple loop to a for-each loop like so:
for (TimerTask t : c)
It is so much more beautiful, and when you read “:” as “in” it should be easy to remember. ”For each TimerTask t in c do…”.