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

Difficults using Iterator

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Could you comment my code? I did not understand why I use while...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while loop is un-necessary here. your for loop do the same conditional check .

your way of iterating the elements in list is Ok . but I do suggest you to go for for-each loop which is introduced in java1.5 because it is simple to use.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote:I did not understand why I use while...

There's not much to say--"while" basically means "as long as". "As long as the iterator has more elements, do whatever is inside the block."
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're mixing up two ways of doing the same thing. Either of the following would work:


The second is probably easier to understand, but the first avoids the need to have a separate assignment statement, which I think is why some people use it.

The third approach, that Seetharaman gives above, is preferable to either of them, though.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Oh, I didn't even register the duplication, probably due to the random indentation.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic