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 For each 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "For each loop" Watch "For each loop" New topic
Author

For each loop

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5402
    
  13

(Question Java Tiger)



concern the code in bold: i guess the method values() is executed once (at the beginning of the loop) and not every time a new value is assigned to variable kolom, because that would be a performance problem


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Values is only called once (in order to get at an Iterator).
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The values() method returns a Collection, and to iterate through that collection an Iterator is obtained from kolommen.values().iterator(). That's done only once, exactly as in the first part of a normal for loop:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5402
    
  13

Originally posted by Barry Gaunt:
The values() method returns a Collection, and to iterate through that collection an Iterator is obtained from kolommen.values().iterator(). That's done only once, exactly as in the first part of a normal for loop:


that's exactly what my .class file looks like after i decompiled it (i thought too late about that option to verify my ideas (it's almost weekend so the brain is slowing down )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For each loop