aspose file tools*
The moose likes Beginning Java and the fly likes new for loop syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new for loop syntax" Watch "new for loop syntax" New topic
Author

new for loop syntax

Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
while im familiar with the regular for-loop i.e.



i recently come accross a for loop that looked like the following...



the above confuses me, can any one explain how the latter for-loop works?


MV
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The enhanced for-loop is a new construct that does what the foreach construct does in other languages.

It allows you to step through an array or collection without all the syntax of a normal for loop.

Here is an example of using the new for loop.



The first loop says, for each int in the array numbers, print the int.

The second for loops says, for each String in the list, print the String.
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
It is an enhanced for loop that was introduced with java 5. You can think of it like a foreach (if you're familiar with foreach from scripting languages).

Check out http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/index.html

for some info on that.


Mike Noel
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
Thanks Guys
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Keith Lynn:
It allows you to step through an array or collection without all the syntax of a normal for loop.

s/collection/java.lang.Iterable
Also note that the index is not available within the loop.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new for loop syntax
 
Similar Threads
for and while loops
Boolean in for loop??
Scriptlet and Expression!!!
for loop
why this code prints 5678?