问题描述
这两个接口有什么区别? 比使用?
What is the exact difference between these two interfaces? Does Enumeration
have benefits over using Iterator
? If anyone could elaborate, a reference article would be appreciated.
推荐答案
查看Java API规范中的:
Looking at the Java API Specification for the Iterator
interface, there is an explanation of the differences between Enumeration
:
- 迭代器允许调用者在迭代期间从底层
集合中删除元素,
底线是枚举
和 Iterator
将给出连续的元素,但 Iterator
以这样的方式改进,因此方法名称更短,并且具有额外的
remove
方法。这是一个并排的比较:
The bottom line is, both Enumeration
and Iterator
will give successive elements, but Iterator
is improved in such a way so the method names are shorter, and has an additional remove
method. Here is a side-by-side comparison:
Enumeration Iterator
---------------- ----------------
hasMoreElement() hasNext()
nextElement() next()
N/A remove()
如Java API规范中所述,对于较新的程序, Iterator
应优先于枚举
,因为Iterator在Java集合框架中取代了枚举。 (从规格。)
As also mentioned in the Java API Specifications, for newer programs, Iterator
should be preferred over Enumeration
, as "Iterator takes the place of Enumeration in the Java collections framework." (From the Iterator
specifications.)
这篇关于Java枚举和迭代器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!