• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question on understand a java snippet

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand a java program, which has a code segment such as



I know the "for" loop should iterate every documents, but I just do not understand how to analyze the logic of this specific snippet
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line uses what's called a foreach construction. The user declares step variable and calls a method that returns a collection. The foreach construction will then traverse the returned elements one at a time, using the step variable to represent the current element as it goes through the loop's statements.

You could say the same thing with a normal for loop. This form of expression is simply more concise.

Declaring the step variable final means the Document reference cannot be changed inside the loop. Some people think it could boost performance, but if it doesn't it does no harm. Personally I find the usage a bit obnoxious.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a longer explanation: The For-Each Loop
 
reply
    Bookmark Topic Watch Topic
  • New Topic