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 Programmer Certification (SCJP/OCPJP) and the fly likes Modifying current slot using foreach 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Modifying current slot using foreach" Watch "Modifying current slot using foreach" New topic
Author

Modifying current slot using foreach

Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
Hi there. I read in MZ Tiger Study guide the following:

"Restrictions on the enhanced for loop:
- CANNOT be used to modify the current slot in a collect or an array. It is not usable for loops where you need to replace elements in a list or array as you traverse it".

However, i managed to compile an run this successfully:



Why?

Thank you.


Leonardo Crespo
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
All you have done is modified the local reference variable name. If you run the following code you will see that the collection has not been changed.



Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
True, thank you Barry!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Modifying current slot using foreach
 
Similar Threads
Static Override?? Please Help...
Anonymous class - i�m very confuse...
I do not want that my button is selected...
Sorting with Comparable
Equals e HashCode