aspose file tools*
The moose likes Beginning Java and the fly likes about Enhanced For loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about Enhanced For loop" Watch "about Enhanced For loop" New topic
Author

about Enhanced For loop

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi,

how can i loop 5 times in enhanced for loop...i think this is a drawback of enhanced for loop...

for(int i=o;i<=5;i++){
}// in enhanced for loop,how can i do this?...

in enhanced for loop...you need array or collection for looping right?

...please can any one explain me on this
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
The enhanced for loop is not designed for looping a specific number of times - it is designed for looping once for each item in a Collection. If you want to loop a specific number of times, use the original for loop format.


Joanne
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


in enhanced for loop...you need array or collection for looping right?

Yes. If you don't have one, you wouldn't use the new syntax. I'm not sure I would call it a drawback, its just different behaviour.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
... and don't use the enhanced for loop if you modify the looped-through collection inside the for-loop's body. Internally, Java uses Iterators for performing the enhanced for-loops, so modifications will result in a ConcurrentModificationException ...

Cheers,
Guido
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
[Guido Sautter]: and don't use the enhanced for loop if you modify the looped-through collection inside the for-loop's body. Internally, Java uses Iterators for performing the enhanced for-loops, so modifications will result in a ConcurrentModificationException ...

Well, your modifications may (quite possibly) result in a CME. It's not guaranteed. Throwing a CME is not a general property of collections - it' s a property of several specific commonly-used collection classes, such as ArrayList, LinkedList, HashSet, and a few others. Even for those, it's never guaranteed that CME will be thrown. But probably it will. So the advice here is sound - it's just expressed a little more strongly than it should be, in my opinion.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Originally posted by Mike Simmons:
Well, your modifications may (quite possibly) result in a CME. It's not guaranteed. Throwing a CME is not a general property of collections - it' s a property of several specific commonly-used collection classes, such as ArrayList, LinkedList, HashSet, and a few others. Even for those, it's never guaranteed that CME will be thrown. But probably it will. So the advice here is sound - it's just expressed a little more strongly than it should be, in my opinion.


I know CME are not guaranteed, the JavaDoc of almost every class in the JCF states this, but you're going to have this sort of trouble in practice. Even though a CME might not be thrown in some circumstances, in would be a concurrecnt modification to alternate a Collection in an enhanced for-lopp running over that very Collection, if resulting in a CME or not ...
[ July 22, 2008: Message edited by: Guido Sautter ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
As I said: "but probably it will". We're in agreement at this point.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about Enhanced For loop