• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question on understand a java snippet  RSS feed

 
cake naiyou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a longer explanation: The For-Each Loop
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!