• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Query on enhanced FOR loop

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I am new to the enhanced for statement.
Considering this code:

Per my understanding this translates to:

Is this correct? Even if it is, I am still not clear how does enhanced for statement work.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about if we rename some variables like this:
?

Can't make it much simpler than that.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your second for loop:
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've always considered the "Enhanced For Loop" as a "for each" loop. Uh oh, am I showing my .NET background?

Ok, so I code in many things... Getting back to your question though

an easier way to understand the Enhanced For Loop is in the for each context.


for(int item : itemContainer[])

So there you are saying "For every int in the array container, put a copy of it into item and give me the result in this loop"

i.e.

itemContainer[0]
itemContainer[1]
itemContainer[2]
itemContainer[3]

means that your loop would execute 4 times and each time

"item" would equal the respective array item above.
[ May 09, 2007: Message edited by: Rob Mech ]
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,Barry for your replies. Now I think I have a better understanding of the enhanced for loop. One question still though:


Does this mean that someNumbers should always be an array. Can it be a collection of some type? If yes, then what should be 'number' here?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


for (int number : someNumbers)

Does this mean that someNumbers should always be an array. Can it be a collection of some type? If yes, then what should be 'number' here?



for(type args1 : args2)

We know it as compatible type. For each object or primitive returned by arg2, the arg1 must be compatible to that.



All compile and run fine.


Thanks,
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying! Actually what I meant was that could we use collections(like HashMap etc.) in the enhanced for loop.

Here, would this work and what type is 'obj'?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You may try this:



Or in type safe way:





Thanks,
[ May 10, 2007: Message edited by: Chandra Bhatt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another simple collection example:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an Integer-valued HashMap you can simply do:

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry can you please explain what does this do:

Is this some new feature?
[ May 10, 2007: Message edited by: Satya Maheshwari ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satya Maheshwari:
Barry can you please explain what does this do:

Is this some new feature?

[ May 10, 2007: Message edited by: Satya Maheshwari ]



Not special, but new: Angelika Langer's Generic FAQ
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic