Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a new way to do the for loop

 
Ashlon Hill
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a new way to do the for loop. My question is, when did this get implemented. How is it better than doing it the old way?

for(Task t : taskList){
JSONObject o = new JSONObject();
o.put("id", t.getId());
o.put("name", t.getName());
array.add(o);
}

thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This "new" for loop was added with Java 5.... So... it was added sometime before 2004. Maybe it is time to stop calling this loop, a "new" or "enhanced" for loop?

Henry
 
Ashlon Hill
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how is this way better than the original for loop?
 
Henry Wong
author
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to answer your question... This "for" loop is *not* a replacement for the previous "for" loop. It is *not* better. It is different. You use it when you just want to iterate linearly through a collection, or linearly through an array, and there is no need to modify the array or collection.

The advantage in these cases is that you don't have to deal with an index or an iterator. The disadvantage is that you don't have access to an index or an iterator, which is why it is used for very specific cases.

Henry
[ August 27, 2008: Message edited by: Henry Wong ]
 
Hendy Setyo Mulyo
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
The advantage in these cases is that you don't have to deal with an index or an iterator. The disadvantage is that you don't have access to an index or an iterator, which is why it is used for very specific cases.


Do not forget that this "enhanced" loop gives automatic class-cast for each Task object in taskList.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hendy Setyo Mulyo:


Do not forget that this "enhanced" loop gives automatic class-cast for each Task object in taskList.


So does the old style for loop as long as you genericized your Collection.



Iterator would work the same. No casting necessary.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15366
40
Android IntelliJ IDE Java Scala Spring
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only a difference in syntax. The compiler turns the "old" for-loop into the "new" for-loop.

Some test code:

I've compiled this, and then ran it through JAD. The results:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
The compiler turns the "old" for-loop into the "new" for-loop.
You mean turns the "new" for loop into the "old" for-loop?

You would expect an iterator; it says that the Iterable<T> interface was introduced to support the enhanced for loop, and Iterable<T> only has one method: iterator().
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You mean turns the "new" for loop into the "old" for-loop?

Eh yes, of course

You would expect an iterator; it says that the Iterable<T> interface was introduced to support the enhanced for loop, and Iterable<T> only has one method: iterator().

I see an Iterator in my code, and the iterator() method. Don't you?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One minor side effect is that you don't need to change the for loop at all if you change the type of the variable from an array to a collection or vise versa.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic