Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

for iterator

I've just come across this code:

Iterator myIterator = myArrayList.iterator();
for (; myIterator.hasNext(); ){
    String string = (String) myIterator.next();
    // do stuff
}

Open in new window


Is there a reason for using a for loop as opposed to a while loop?
0
allelopath
Asked:
allelopath
3 Solutions
 
ksivananthCommented:
can't think of any... IMO, "while" suits best here!
0
 
for_yanCommented:


type "for loop vs while loop Java" in Google  - tons of links and tons of opinions
0
 
for_yanCommented:
I'd also think that when you don't have known number of iterations but rather logical condition
while is more suitable, when I have number I rather use for - easier to read and understand
0
 
objectsCommented:
Its really personal preference

these days you can simplify it to

for (String string : myArrayList) {
    // do stuff
0
 
allelopathAuthor Commented:
>>these days you can simplify
ah, that's why it was done. It is old code, probably written before enhanced for loops
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now