aspose file tools*
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
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: 4637
    
    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: 4637
    
    5

Thanks for the clarification.
In my case, the new style is great 99.44% of the time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing iterator in enhanced for construct
 
Similar Threads
for-each version for
is this final variable modified
Enhanced For Loop Problem
Reading & Writing to text file using ArrayList incorrectly
doubt in the advance for loop in java6