所以我目前正在将一些使用 Vector
的旧代码升级到 ArrayList
中,并且不确定使用什么来替换旧的 vector 段。
public Enumeration enumerateMapping() {
return mappingList.elements();
}
我需要将 .elements()
替换为 ArrayList
等效项。如果有人碰巧知道这些内容的翻译链接,那也会非常有帮助。
最佳答案
ArrayList
(和大多数 Collection
s)实现 Iterable
,所以你应该返回 iterator()
:
public Iterator enumerateMapping() {
return mappingList.iterator();
}
最好使用泛型类型而不是原始类型,即 Iterator<ElementType>
.
作为Enumeration
Javadoc建议,推荐使用Iterator
而不是Enumeration
:
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
关于java - 如果该方法返回枚举,则从 ArrayList 返回什么值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831599/