Difference between moving an Iterator forward with a for statement and a while statement

前端 未结 5 2091
别跟我提以往
别跟我提以往 2021-01-11 10:16

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):

I         


        
5条回答
  •  温柔的废话
    2021-01-11 10:18

    It's just a style thing and like you I prefer the for loop when using something with an index. If you're using Java 5 you should of course use a foreach loop over the collection anyway:

    Collection someCollection = someCollectionOfString();
    for (String element : someCollection) {
    ....
    }
    

提交回复
热议问题