增强的for循环不接受迭代器 [英] Enhanced for-loop does not accept Iterator

查看:67
本文介绍了增强的for循环不接受迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果以前有人问过这个问题.我的搜索没有提出任何其他类似的问题.这使我在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循环被设计为以这种方式工作.尽管It​​erator不是集合,但可用于一次从集合返回一个元素.请注意,java.lang.Iterable接口中的唯一方法是Iterator<T> 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循环是

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

  1. 为什么我不能在Iterator中使用增强的for语句(而是 而不是Iterable或数组)?
  1. Why can't I use the enhanced for statement with an Iterator (rather than an Iterable or array)?

两个原因:(1)构造不会 如果您有语法上的改进,可以提供很多方法 您代码中的显式迭代器,以及(2)执行循环将 有副作用"(提前(通常是精疲力竭)) 迭代器.换句话说,增强的for语句提供了一个 简单,优雅的解决方案,适用于迭代一个 集合或数组,并且不尝试解决更复杂的问题 案例,使用传统的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.

  1. 为什么我不能使用增强的for语句来:
    • 在遍历集合时会删除元素(过滤")吗?
    • 同时迭代多个集合或数组吗?
    • 修改数组或列表中的当前插槽吗?
  1. Why can't I use the enhanced for statement to:
    • remove elements as I traverse a collection ("filtering")?
    • simultaneously iterate over multiple collections or arrays?
    • modify the current slot in an array or list?

请参阅上面的项目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.

这篇关于增强的for循环不接受迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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