Java 为什么枚举不可迭代?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why aren't Enumerations Iterable?
提问by SCdF
In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable
:
在 Java 5 及更高版本中,您有 foreach 循环,它可以神奇地用于任何实现Iterable
:
for (Object o : list) {
doStuff(o);
}
However, Enumerable
still does not implement Iterable
, meaning that to iterate over an Enumeration
you must do the following:
但是,Enumerable
仍然没有实现Iterable
,这意味着要迭代一个,Enumeration
您必须执行以下操作:
for(; e.hasMoreElements() ;) {
doStuff(e.nextElement());
}
Does anyone know if there is a reason why Enumeration
still does not implement Iterable
?
有谁知道是否有原因Enumeration
仍然不实施Iterable
?
Edit:As a clarification, I'm not talking about the language concept of an enum, I'm talking a Java-specific class in the Java API called 'Enumeration'.
编辑:作为澄清,我不是在谈论enum的语言概念,我在谈论 Java API 中名为“ Enumeration”的特定于 Java 的类。
采纳答案by Blorgbeard is out
Enumeration hasn't been modified to support Iterable because it's an interface not a concrete class (like Vector, which was modifed to support the Collections interface).
枚举没有被修改为支持 Iterable,因为它是一个接口而不是一个具体的类(比如 Vector,它被修改为支持 Collections 接口)。
If Enumeration was changed to support Iterable it would break a bunch of people's code.
如果将 Enumeration 更改为支持 Iterable,它将破坏一堆人的代码。
回答by dlinsin
AFAIK Enumeration is kinda "deprecated":
AFAIK 枚举有点“不推荐”:
Iterator takes the place of Enumeration in the Java collections framework
迭代器取代了 Java 集合框架中的枚举
I hope they'll change the Servlet API with JSR 315 to use Iterator instead of Enumeration.
我希望他们将使用 JSR 315 更改 Servlet API 以使用 Iterator 而不是 Enumeration。
回答by erickson
It doesn't make sense for Enumeration
to implement Iterable
. Iterable
is a factory method for Iterator
. Enumeration
is analogous to Iterator
, and only maintains state for a single enumeration.
Enumeration
实施没有意义Iterable
。Iterable
是 的工厂方法Iterator
。Enumeration
类似于Iterator
,并且只维护单个枚举的状态。
So, be careful trying to wrap an Enumeration
as an Iterable
. If someone passes me an Iterable
, I will assume that I can call iterator()
on it repeatedly, creating as many Iterator
instances as I want, and iterating independently on each. A wrapped Enumeration
will not fulfill this contract; don't let your wrapped Enumeration
escape from your own code. (As an aside, I noticed that Java 7's DirectoryStream
violates expectations in just this way, and shouldn't be allowed to "escape" either.)
因此,尝试将 an 包装Enumeration
为Iterable
. 如果有人传给我一个Iterable
,我会假设我可以iterator()
重复调用它,创建任意数量的Iterator
实例,并在每个实例上独立迭代。包裹Enumeration
将不履行本合同;不要让你的包裹Enumeration
逃脱你自己的代码。(DirectoryStream
顺便说一句,我注意到 Java 7以这种方式违反了预期,也不应该被允许“逃避”。)
Enumeration
is like an Iterator
, not an Iterable
. A Collection
is Iterable
. An Iterator
is not.
Enumeration
就像一个Iterator
,而不是一个Iterable
。ACollection
是Iterable
。安Iterator
不是。
You can't do this:
你不能这样做:
Vector<X> list = …
Iterator<X> i = list.iterator();
for (X x : i) {
x.doStuff();
}
So it wouldn't make sense to do this:
所以这样做是没有意义的:
Vector<X> list = …
Enumeration<X> i = list.enumeration();
for (X x : i) {
x.doStuff();
}
There is no Enumerable
equivalent to Iterable
. It could be added without breaking anything to work in for loops, but what would be the point? If you are able to implement this new Enumerable
interface, why not just implement Iterable
instead?
没有Enumerable
等价于Iterable
。可以在不破坏任何内容的情况下添加它以在 for 循环中工作,但重点是什么?如果您能够实现这个新Enumerable
接口,为什么不直接实现Iterable
呢?
回答by Tom Hawtin - tackline
As an easy and cleanway of using an Enumeration with the enhanced for loop, convert to an ArrayList with java.util.Collections.list.
作为将Enumeration 与增强的 for 循环一起使用的一种简单而干净的方法,请使用 java.util.Collections.list 转换为 ArrayList。
for (TableColumn col : Collections.list(columnModel.getColumns()) {
(javax.swing.table.TableColumnModel.getColumns returns Enumeration.)
(javax.swing.table.TableColumnModel.getColumns 返回枚举。)
Note, this may be very slightly less efficient.
请注意,这可能会稍微降低效率。
回答by user5071455
If you would just like it to be syntactically a little cleaner, you can use:
如果你只是想让它在语法上更简洁,你可以使用:
while(e.hasMoreElements()) {
doStuff(e.nextElement());
}