Hi JavaRanch folk,
I have heard (read) many times that one must not
1. modify
2. delete
an element from a list when iterating over it using the enhanced for loop, for example
http://jmonkeyengine.com/mark/?p=147. I am clear with the second point of the rule: not to delete an element from a list while iterating over it. But I never came across a suitable example illustrating the 'modify' operation on the list. Specifically I would like to know whether the following use of enhanced for loop is a valid one.
The code above runs fine and prints a list whose every element has the instance variable 'val' set to the integer 2, which is what I expected to happen. But I don't know whether this code is working fine as a special case or is it the norm. I will repeat my question here: Have I transgressed the rule to not to modify a list while iterating over it using the enhanced for loop?
Now my second question is: If the code above doesn't illustrate the wrong practice of modifying a list while iterating over it using the enhanced for loop then what does?
Thanks
Kind regards,
Ganesh
[ May 04, 2008: Message edited by: Ganesh Hegde ]
[ May 04, 2008: Message edited by: Ganesh Hegde ]