增强的 for 循环不接受 Iterator
对不起,如果以前有人问过这个问题.我的搜索没有提出任何其他类似的问题.这在 Java 中让我感到惊讶.
Excuse me if this has been asked before. My search did not bring up any other similar question. This is something that surprised me in Java.
显然,增强的 for 循环只接受数组或 java.lang.Iterable
的实例.它不接受 java.util.Iterator
作为有效的 obj 引用来迭代.例如,Eclipse 显示以下代码的错误消息.它说:只能遍历数组或 java.lang.Iterable 的实例"
Apparently, the enhanced for-loop only accepts an array or an instance of java.lang.Iterable
. It does not accept a java.util.Iterator
as a valid obj reference to iterate over. For example, Eclipse shows an error message for the following code. It says: "Can only iterate over an array or an instance of java.lang.Iterable"
Set<String> mySet = new HashSet<String>();
mySet.add("dummy");
mySet.add("test");
Iterator<String> strings = mySet.iterator();
for (String str : strings) {
// Process str value ...
}
为什么会这样?我想知道为什么增强的 for 循环是这样设计的.尽管 Iterator 不是集合,但它可用于从集合中一次返回一个元素.请注意,java.lang.Iterable
接口中的唯一方法是 Iterator
返回一个 Iterator
.在这里,我直接给它一个Iterator
.我知道 hasNext()
和 next()
可以使用,但是使用增强的 for 循环使它看起来更干净.
Why would this be so? I want to know why the enhanced for-loop was designed to work this way. Although an Iterator is not a collection, it can be used to return one element at a time from a collection. Note that the only method in the java.lang.Iterable
interface is Iterator<T> iterator()
which returns an Iterator
. Here, I am directly handing it an Iterator
. I know that hasNext()
and next()
can be used but using the enhanced for-loop makes it look cleaner.
我现在明白的一件事是我可以直接在 mySet
上使用增强的 for 循环.所以我什至不需要额外的调用来获取 Iterator
.所以,这将是编码的方式,是的 - 它确实有一些意义.
One thing I understand now is that I could use the enhanced for-loop directly over mySet
. So I don't even need the extra call to get an Iterator
. So, that would be the way to code this, and yes - it does make some sense.
推荐答案
增强的 for 循环是 JSR 201.从该页面,您可以下载拟议的最终草案文件,其中包括直接解决您的问题的常见问题解答部分:
The enhanced for loop was part of JSR 201. From that page, you can download the proposed final draft documents, which include a FAQ section directly addressing your question:
附录 I. 设计常见问题解答
Appendix I. Design FAQ
- 为什么我不能将增强的 for 语句与迭代器一起使用(而是比 Iterable 或数组)?
两个原因:(1)构造不会如果你有一个代码中的显式迭代器,以及 (2) 循环的执行将具有推进(并且通常使人筋疲力尽)的副作用"迭代器.换句话说,增强的 for 语句提供了一个简单、优雅的解决方案,用于迭代 a 的常见情况集合或数组,并没有尝试处理更复杂的用传统的 for 语句更好地解决这些情况.
Two reasons: (1) The construct would not provide much in the way on syntactic improvement if you had an explicit iterator in your code, and (2) Execution of the loop would have the "side effect" of advancing (and typically exhausting) the iterator. In other words, the enhanced for statement provides a simple, elegant, solution for the common case of iterating over a collection or array, and does not attempt to address more complicated cases, which are better addressed with the traditional for statement.
- 为什么我不能使用增强的 for 语句来:
- 在我遍历集合时删除元素(过滤")?
- 同时迭代多个集合或数组?
- 修改数组或列表中的当前槽?
参见上面的第 1 项.专家组考虑了这些案例,但选择了一个简单、干净的扩展,可以很好地处理(原文如此)一件事.这设计遵循80-20 规则"(它处理 80% 的案例,其中 20%努力).如果一个案例属于另外 20%,您可以随时使用像您过去所做的那样显式迭代器或索引.
See Item 1 above. The expert group considered these cases, but opted for a simple, clean extension that dose(sic) one thing well. The design obeys the "80-20 rule" (it handles 80% of the cases with 20% of the effort). If a case falls into the other 20%, you can always use an explicit iterator or index as you've done in the past.
换句话说,委员会选择限制提案的范围,而一些人们可以想象成为提案一部分的功能并没有被淘汰.
In other words, the committee chose to limit the scope of the proposal, and some features that one could imagine being part of the proposal didn't make the cut.
相关文章