使用for语句和while语句将Iterator向前移动之间的区别 [英] Difference between moving an Iterator forward with a for statement and a while statement

查看:249
本文介绍了使用for语句和while语句将Iterator向前移动之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用对象的Iterator时,我使用while循环(正如在每本学习Java的书中所写的,就像Bruce Eckel的 Thinking in Java 一样):

  Iterator it = ... 

while(it.hasNext()){
// ...
}

但是有时候我看到有人使用循环



pre $ Iterator it = ...
for(Iterator it = ...; it .hasNext();){
// ...
}

我不理解这个选择:


  • 我使用 for循环序列(如数组)或步骤的特殊规则(一般声明为简单增量 counter ++ )。

  • 当循环结束时,我使用while循环,但我没有这个约束,只有退出的逻辑条件。


这是一个没有其他原因的样式编码问题,或者它存在一些我不知道的逻辑(比如性能)? b
$ b

感谢您的反馈

解决方案

  for(Iterator it = ...; it.hasNext();){
// ...





$(在你的代码中的前面的声明是多余的,以及额外的分号)



无论你使用这个语法还是,而循环都是一个趣味问题,到完全一样。 for循环的一般语法是:< init stmt>;< loop cond>;< iterate stmt>){< body>; }

相当于:

 < init stmt>; $(< loop cond>){< body>; < iterate stmt>;其实,以上两种形式并不完全等价,如果(如在问题中)变量是用init语句声明的。在这种情况下,迭代器变量的范围将会有所不同。在for循环中,范围仅限于循环本身,在while循环的情况下,范围扩展到封闭块的末尾(没有什么大惊喜,因为声明在循环之外)另外,正如其他人已经指出的那样,在较新版本的Java中,for循环有一个简写符号:


$ b pre> for(Iterator< Foo> it = myIterable.iterator(); it.hasNext();){
Foo foo = it.next();
// ...
}

可以写成: (Foo foo:myIterable){
// ...
}
<$ p
$ b

  code> 

使用这种形式,您当然会失去对迭代器的直接引用,这是必要的,例如,如果要删除迭代时,来自集合的项目。


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

解决方案

The correct syntax for the for loop is:

for (Iterator it = ...; it.hasNext(); ){
    //...
}

(The preceding declaration in your code is superfluous, as well as the extra semicolon in the for loop heading.)

Whether you use this syntax or the while loop is a matter of taste, both translate to exactly the same. The generic syntax of the for loop is:

for (<init stmt>; <loop cond>; <iterate stmt>) { <body>; }

which is equivalent to:

<init stmt>;
while (<loop cond>) { <body>; <iterate stmt>; }

Edit: Actually, the above two forms are not entirely equivalent, if (as in the question) the variable is declared with the init statement. In this case, there will be a difference in the scope of the iterator variable. With the for loop, the scope is limited to the loop itself, in the case of the while loop, however, the scope extends to the end of the enclosing block (no big surprise, since the declaration is outside the loop).

Also, as others have pointed out, in newer versions of Java, there is a shorthand notation for the for loop:

for (Iterator<Foo> it = myIterable.iterator(); it.hasNext(); ) {
    Foo foo = it.next();
    //...
}

can be written as:

for (Foo foo : myIterable) {
    //...
}

With this form, you of course lose the direct reference to the iterator, which is necessary, for example, if you want to delete items from the collection while iterating.

这篇关于使用for语句和while语句将Iterator向前移动之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆