aspose file tools*
The moose likes Beginning Java and the fly likes a new way to do the for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a new way to do the for loop" Watch "a new way to do the for loop" New topic
Author

a new way to do the for loop

David Gill
Greenhorn

Joined: Feb 24, 2005
Posts: 19
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Gill
Greenhorn

Joined: Feb 24, 2005
Posts: 19
So how is this way better than the original for loop?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Dec 01, 2004
Posts: 219

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.


Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

See this: The For-Each Loop


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a new way to do the for loop