现在的Java程序员习惯使用Iterator<E>接口或者增强for循环来遍历集合。如果回到JDK 1.0,Enumeration接口则是遍历向量、哈希表的不二之选。本节就解读和翻译一下Enumeration<E>接口的源代码。当然,有一点还是得再强调一下:Enumeration<E>的泛型实在JDK 1.5时加上去的,最初的版本是没有泛型的。

package java.util;

/**
* An object that implements the Enumeration interface generates a
* series of elements, one at a time(一次一个). Successive calls to the
* <code>nextElement</code> method return successive(逐个) elements of the
* series.
* 实现了Enumeration接口的对象自动生成一系列元素,一次一个。
* 逐次调用nextElement()方法,依次返回系列元素。
* <p>
* For example, to print all elements of a <tt>Vector&lt;E&gt;</tt> <i>v</i>:
* 举例而言,打印Vector<V>中的所有元素
* <pre>
* for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
* System.out.println(e.nextElement());</pre>
* <p>
* Methods are provided to enumerate through the elements of a
* vector, the keys of a hashtable, and the values in a hashtable.
* Enumerations are also used to specify the input streams to a
* <code>SequenceInputStream</code>
* <p>
* 所有的方法都被提供来枚举Vector、hashtable中的键、值的元素。
* Enumeration<E>也被用来指定一个到SequenceInputStream的输入流。
*
* 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.
* 提示:Enumeration<E>接口的功能已经被Iterator<E>接口赋复制。除此之外,Iterator<E>接口
* 添加了一个可选删除(元素)操作,以及更加简短的方法名。新的实现应该优先考虑使用Iterator<E>
* 接口,而非Enumeration<E>。
*
* @see java.util.Iterator
* @see java.io.SequenceInputStream
* @see java.util.Enumeration#nextElement()
* @see java.util.Hashtable
* @see java.util.Hashtable#elements()
* @see java.util.Hashtable#keys()
* @see java.util.Vector
* @see java.util.Vector#elements()
*
* @author Lee Boynton
* @since JDK1.0 历史遗留问题
*/
public interface Enumeration<E> {
/**
* Tests if this enumeration contains more elements.
* 测试是否此枚举还包含有元素。
* @return <code>true</code> if and only if this enumeration object
* contains at least one more element to provide;
* <code>false</code> otherwise.
* 当且仅当此枚举对象还有至少一个元素可以提供,返回true;否则,返回false。
*/
boolean hasMoreElements(); // 别少了s,老外写代码是很重视单复数滴 /**
* Returns the next element of this enumeration if this enumeration
* object has at least one more element to provide.
* 如果此枚举对象还有至少一个元素可以提供,那么返回此枚举的下一个元素。
* @return the next element of this enumeration.
* 返回此枚举的下一个元素
* @exception NoSuchElementException if no more elements exist.
* 如果没有更多的元素存在,抛出NoSuchElementException异常
*/
E nextElement();
}

阅读完Enumeration<E>的源代码和翻译之后,想必您和我都有同一个想法:这玩意并没有什么*用。

那么为什么我们还要去了解这个接口的源代码呢?作用就是、、、能够装逼。

回归JDK源代码(2)Enumeration<E>接口的更多相关文章

  1. 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现

    本文转载自http://www.ibm.com/developerworks/cn/java/j-lo-tree/ 目录: TreeSet 和 TreeMap 的关系 TreeMap 的添加节点 Tr ...

  2. JDK源代码学习系列04----ArrayList

                                                                             JDK源代码学习系列04----ArrayList 1 ...

  3. JDK源代码学习系列05----LinkedList

                                             JDK源代码学习系列05----LinkedList 1.LinkedList简单介绍 LinkedList是基于双向 ...

  4. 调试JDK源代码-一步一步看HashMap怎么Hash和扩容

    调试JDK源代码-一步一步看HashMap怎么Hash和扩容 调试JDK源代码-ConcurrentHashMap实现原理 调试JDK源代码-HashSet实现原理 调试JDK源代码-调试JDK源代码 ...

  5. JDK源代码学习系列03----StringBuffer+StringBuilder

                         JDK源代码学习系列03----StringBuffer+StringBuilder 因为前面学习了StringBuffer和StringBuilder的父类 ...

  6. 无法debug断点跟踪JDK源代码——missing line number attributes的解决方法

    在项目工程->Properties->Java Build Path->Libraries中导入的JRE System Library库里,给jar包添加JDK源代码包后,能够直接打 ...

  7. JDK源代码学习系列07----Stack

                                                                   JDK源代码学习系列07----Stack 1.Stack源代码很easy ...

  8. eclipse Mars查看JDK源代码

    eclipse Mars查看JDK源代码 问题描写叙述,eclipse(mars)下看不到JDK类的声明即源代码部分的内容. 如图右击string类型: 点击打开声明.结果出现了下图所看到的的错误,无 ...

  9. 如何调试JDK源代码并查看局部变量值

    如下图: 按F5进入String.startsWith,如下: 点“Edit Source Lookup Path” 附加源代码,如下图: 附加上源代码后如下: 可以看到,当鼠标放在“prefix”上 ...

随机推荐

  1. 【记录一下】从0到1 我的python开发之路

    请设计实现一个商城系统,商城主要提供两个功能:商品管理.会员管理. 商品管理: - 查看商品列表 - 根据关键字搜索指定商品 - 录入商品 会员管理:[无需开发,如选择则提示此功能不可用,正在开发中, ...

  2. 使用windows服务修改CPU型号(重启依然有效)

    此项目基于.net framework 4.0 效果如下: 服务运行前: 服务运行后: 思路大概是这样: 通过修改注册表可以修改CPU型号,把服务设置成本地服务,并且开机自动启动,来实现开机自动修改处 ...

  3. Codeforces Round #532 (Div. 2)- B(思维)

    Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...

  4. jquery知识点结合使用

    1.在页面的某个具有id的element内插入其他element,并给其增加悬浮提示. $('#id').after('<div id='box'></div>'); $('# ...

  5. hdu2067 小兔的棋盘

    小兔的棋盘 时间限制:1000/1000 MS(Java / Others)内存限制:32768/32768 K(Java / Others)总提交内容:13029接受的提交内容:6517 问题描述 ...

  6. ZPL打印机命令解释

    个人备忘: 1.装驱动,装驱动要装对应的ZPL或者EPL版本,目前发现GK888T无需选择,直接装GK888T即可,其他机型未知. 2.标签设计,文本部分用SimSun-ExtB字体,变量内容部分用Z ...

  7. C# 事务提交(非数据库)

    .Net 2.0开始支持 static void Main(string[] args) { using (TransactionScope ts = new TransactionScope()) ...

  8. 5.监听器(Listener)

    1.监听器简介: 监听器主要用来监听对象的创建,属性的变化,是一个实现特定接口的普通Java类. Listener接口与事件对应表: 与 ServletContext 有关 ServletContex ...

  9. c# yield关键字原理

    https://www.cnblogs.com/blueberryzzz/p/8678700.html c# yield关键字原理详解 1.yield实现的功能yield return:先看下面的代码 ...

  10. my.变身卡

    中级 1620 (4--6级) 高级 8323 (7--9级) 赤炎: 1.老鼠精 135 / 150 2.白骨精 750 3.情丝娘子 264 / 294 4.沙和尚 500 5.九头虫 1835 ...