This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
But to answer your question... This "for" loop is *not* a replacement for the previous "for" loop. It is *not* better. It is different. You use it when you just want to iterate linearly through a collection, or linearly through an array, and there is no need to modify the array or collection.
The advantage in these cases is that you don't have to deal with an index or an iterator. The disadvantage is that you don't have access to an index or an iterator, which is why it is used for very specific cases.
Henry [ August 27, 2008: Message edited by: Henry Wong ]
Originally posted by Henry Wong: The advantage in these cases is that you don't have to deal with an index or an iterator. The disadvantage is that you don't have access to an index or an iterator, which is why it is used for very specific cases.
Do not forget that this "enhanced" loop gives automatic class-cast for each Task object in taskList.
One minor side effect is that you don't need to change the for loop at all if you change the type of the variable from an array to a collection or vise versa.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus