This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • 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
 
Master Rancher
Posts: 5060
81
  • 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
Master Rancher
Posts: 5060
81
  • 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
 
You don't like waffles? Well, do you like this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic