为什么枚举不可以进行? [英] Why aren't Enumerations Iterable?
问题描述
在Java 5及以上版本中,你有foreach循环,它可以实现任何实现 Iterable
:
In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable
:
for (Object o : list) {
doStuff(o);
}
然而,可枚举
仍然没有实现 Iterable
,这意味着迭代 Enumeration
你必须执行以下操作:
However, Enumerable
still does not implement Iterable
, meaning that to iterate over an Enumeration
you must do the following:
for(; e.hasMoreElements() ;) {
doStuff(e.nextElement());
}
有没有人知道是否有枚举的原因
仍然没有实现 Iterable
?
Does anyone know if there is a reason why Enumeration
still does not implement Iterable
?
编辑作为澄清,我不是在谈论枚举的语言概念,我正在谈论Java API中特定于Java的类,名为枚举。
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'.
推荐答案
枚举尚未修改为支持Iterable,因为它是一个不是具体类的接口(如Vector,它被修改为支持Collections接口)。
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).
如果Enumeration被更改为支持Iterable,它会破坏一堆人的代码。
If Enumeration was changed to support Iterable it would break a bunch of people's code.
这篇关于为什么枚举不可以进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!