The old days of writing the same for...loop code with an iterator or index that you increment are mostly gone, provided you don't need to access the iterator or index.

for (int i=0; i<items.size; i++)
{
    Item item = items[i];
    if (item.isSold())
    {
        :
    }
}

for (Iterator i=books.iterator(); i.hasNext(); )
{
    Book book = (Book)i.next();
    if (book.isSold())
    {
        :
    }
}

..and the same code using Java 1.5 upwards:

for (Item item : items)
{
    if (item.isSold())
    {
        :
    }
}

for (Book book : books)
{
    if (book.isSold())
    {
        :
    }
}

Note the lack of a cast or generic definition in Book book : books.

blog comments powered by Disqus