Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

1.5 style "for" loops

Thread view: 
Chanchal - 28 Nov 2005 05:51 GMT
hey,

can some one tell me what is the advantage of "for each" style loop in
1.5 over the conventional for loop
thanx

chanchal
Adam Maass - 28 Nov 2005 06:02 GMT
> hey,
>
[quoted text clipped - 3 lines]
>
> chanchal

It's a briefer syntax:

Old style:

for(Iterator i = collection.iterator(); i.hasNext(); ;) {
  Item item = (Item)i.next();
  ...
}

New style:

for(Item item: collection) {
  ...
}

IMHO, the new style is conceptually a bit cleaner as well: you're applying
the body of the loop to each item in the collection in turn. You don't have
to write code for the mechanics of getting that to happen.

-- Adam Maass
Thomas Hawtin - 28 Nov 2005 13:01 GMT
>>can some one tell me what is the advantage of "for each" style loop in
>>1.5 over the conventional for loop

> It's a briefer syntax:

And more consistent between array and non-RandomAccess Iterables.

> Old style:
>
> for(Iterator i = collection.iterator(); i.hasNext(); ;) {
>    Item item = (Item)i.next();
>    ...
> }

Which does conceptually the same thing as the completely different code:

final int num = array.length;
for (int ct=0; ct<num; ++ct) {
    Item item = array[ct];
    ...
}

The new syntax does fall down if you want to remove items from an
iterator, replace items or use the index. But, say, 80% of the time it
just reads better.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Thomas Fritsch - 28 Nov 2005 11:04 GMT
> can some one tell me what is the advantage of "for each" style loop in
> 1.5 over the conventional for loop
Quoted from
<http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#forloop> :
"...eliminates the drudgery and error-proneness of iterators and index
variables when iterating over collections and arrays"

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Roedy Green - 28 Nov 2005 17:14 GMT
>can some one tell me what is the advantage of "for each" style loop in
>1.5 over the conventional for loop

they both generate the same code.  The for:each is terser and hence
easier to maintain and read.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Benji - 28 Nov 2005 17:17 GMT
>>can some one tell me what is the advantage of "for each" style loop in
>>1.5 over the conventional for loop

> they both generate the same code.

That's not necessarily true.  Using a for:each gives the compiler more of
a chance to optimize the loop than it would with a standard for.  (Of
course, whether or not it is optimized in the current compiler/VM is
another story)

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.