Java枚举和迭代器的区别 [英] Difference between Java Enumeration and Iterator

查看:21
本文介绍了Java枚举和迭代器的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个接口的确切区别是什么?Enumeration比使用 Iterator?如果有人能详细说明,参考文章将不胜感激.

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.

推荐答案

查看 Iterator接口,有枚举:

迭代器不同于枚举有两种方式:

Iterators differ from enumerations in two ways:

  • 迭代器允许调用者从底层移除元素在迭代期间收集定义明确的语义.
  • 改进了方法名称.

最重要的是,EnumerationIterator 都会给出连续的元素,但是 Iterator 通过缩短文字来改进方法名称,它有一个额外的 remove 方法.这是一个并排比较:

The bottom line is, both Enumeration and Iterator will give successive elements, but Iterator improved the method names by shortening away the verbiage, and it has an additional remove method. Here is a side-by-side comparison:

  Enumeration                     Iterator
  ----------------                ----------------
  hasMoreElements()               hasNext()
  nextElement()                   next()
  N/A                             remove()

Java API 规范中也提到,对于较新的程序,Iterator 应该优先于 Enumeration,因为Iterator 取代了 Java 集合中的 Enumeration"框架."(来自 Iterator 规格.)

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枚举和迭代器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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