• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • 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

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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