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

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

问题描述

当我使用对象的迭代器时,我使用了while 循环(每本学习 Java 的书中都有写,如 Bruce Eckel 的 Thinking in Java):

迭代器 it=...而(it.hasNext()){//...}

但有时我看到有人使用for循环:

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

我不明白这个选择:

  • 我使用 for 循环 当我有一个带有序数序列(作为数组)或带有特殊规则的集合(通常声明为简单的增量 counter++).
  • 当循环结束时我使用 while 循环 我没有这个约束,只有一个退出的逻辑条件.

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

感谢您的每一个反馈

解决方案

for 循环的正确语法是:

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

(代码中前面的声明是多余的,以及 for 循环标题中的额外分号.)

无论您使用这种语法还是使用 while 循环都是一种品味问题,两者都转化为完全相同.for 循环的通用语法是:

for (; ; ) { ;}

相当于:

;while (<loop cond>) { <body>;<迭代语句>;}

实际上,上述两种形式并不完全等效,if(如问题中所示)使用 init 语句声明变量.在这种情况下,迭代器变量的范围会有所不同.对于 for 循环,作用域仅限于循环本身,而在 while 循环中,作用域扩展到封闭块的末尾(没什么奇怪的,因为声明在循环之外).

此外,正如其他人所指出的,在较新版本的 Java 中,for 循环有一个速记符号:

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

可以写成:

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

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

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 语句向前移动迭代器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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