This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

about Enhanced For loop

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic