This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing iterator in enhanced for construct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing iterator in enhanced for construct" Watch "Accessing iterator in enhanced for construct" New topic
Author

Accessing iterator in enhanced for construct

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

When you use the enhanced for, can you access the relative index being iterated.

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No, for that you need to use the old-style for loop. They considered some ways that the for loop might have been made more powerful - access to the index, or to the iterator's remove() method, etc - but instead decided to keep the syntax as simple as possible, as that covers, what, 80% or more of the cases where you use a for loop? And the remaining more complex cases can always be handled by the original for loop syntax.


"I'm not back." - Bill Harding, Twister
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Thanks for the clarification.
In my case, the new style is great 99.44% of the time
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing iterator in enhanced for construct
 
Similar Threads
for-each version for
doubt in the advance for loop in java6
is this final variable modified
Reading & Writing to text file using ArrayList incorrectly
Enhanced For Loop Problem