When I use an Iterator of Object I use a while loop (as written in every book learning Java, as Thinking in Java of Bruce Eckel):
Iterator it=...
while(it.hasNext()){
//...
}
but sometime i saw than instead somebody use the for loop:
Iterator it=...
for (Iterator it=...; it.hasNext();){
//...
}
I don't' understand this choice:
- I use the for loop when I have a collection with ordinal sequence (as array) or with a special rule for the step (declared generally as a simple increment
counter++
). - I use the while loop when the loop finishes with I have'nt this constraints but only a logic condition for exit.
It's a question of style-coding without other cause or it exists some other logic (performance, for example) that I don't' know?
Thanks for every feedback