[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
It means you ought not read the website or book where you found thatsurya.raaj prakash wrote: . . . Iterator is just a clutter,what it mean?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
No, it isn't. I meant the bit about "iterator is a clutter". There is nothing about clutter in the Java™ Tutorials section.Rob Prime wrote:It's from the page Jesper linked to - Oracle's own documentation (back then created by Sun).
SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie wrote:
No, it isn't. I meant the bit about "iterator is a clutter". There is nothing about clutter in the Java™ Tutorials section.Rob Prime wrote:It's from the page Jesper linked to - Oracle's own documentation (back then created by Sun).
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
akash shrimali wrote:the for each loop allows you the features of iteration , loop includes all the features of previous for loop but has its own enhancements like printing the No of elements in array , is smaller in size , easy to use , proper definition for the developer !!
Found it, thank you. It actually saysRob Prime wrote:Jesper posted this link. Check out the piece of text just after the first code block.
. . . which is different from what was originally quoted.The for-each construct gets rid of the clutter and the opportunity for error.
The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP6.0,My blog Ranchers from Delhi
SCJP6.0,My blog Ranchers from Delhi
Even though I used ctrl-F I never found that bit. SorryRob Prime wrote:The three sentences before that are
. . .The iterator is just clutter.
Don't get me started about those stupid light bulbs. |