Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on enhanced FOR loop

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
For your second for loop:
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Another simple collection example:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic