• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about Enhanced For loop

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Guido Sautter
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said: "but probably it will". We're in agreement at this point.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic