File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query on enhanced FOR loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query on enhanced FOR loop" Watch "Query on enhanced FOR loop" New topic
Author

Query on enhanced FOR loop

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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.


Thanks and Regards
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What about if we rename some variables like this:
?

Can't make it much simpler than that.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For your second for loop:
Rob Mech
Ranch Hand

Joined: Apr 05, 2007
Posts: 56
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 ]

Rob Mech, SCJP 1.5<br /><a href="http://www.robsprogrammingjunk.com/" target="_blank" rel="nofollow">http://www.robsprogrammingjunk.com/</a>
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Feb 28, 2007
Posts: 1707

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,


cmbhatt
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Feb 28, 2007
Posts: 1707
Hi,

You may try this:



Or in type safe way:





Thanks,
[ May 10, 2007: Message edited by: Chandra Bhatt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Another simple collection example:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For an Integer-valued HashMap you can simply do:

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Aug 03, 2002
Posts: 7729
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query on enhanced FOR loop