博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
回归JDK源代码(2)Enumeration<E>接口
阅读量:7100 次
发布时间:2019-06-28

本文共 2670 字,大约阅读时间需要 8 分钟。

现在的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 * nextElement method return successive(逐个) elements of the * series.  * 实现了Enumeration接口的对象自动生成一系列元素,一次一个。  * 逐次调用nextElement()方法,依次返回系列元素。 * 

* For example, to print all elements of a Vector<E> v: * 举例而言,打印Vector

中的所有元素 *

 *   for (Enumeration
e = v.elements(); e.hasMoreElements();) * System.out.println(e.nextElement());
*

* 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 * SequenceInputStream *

* 所有的方法都被提供来枚举Vector、hashtable中的键、值的元素。 * Enumeration

也被用来指定一个到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
接口的功能已经被Iterator
接口赋复制。除此之外,Iterator
接口 * 添加了一个可选删除(元素)操作,以及更加简短的方法名。新的实现应该优先考虑使用Iterator
* 接口,而非Enumeration
。 * * @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
{ /** * Tests if this enumeration contains more elements. * 测试是否此枚举还包含有元素。 * @return
true if and only if this enumeration object * contains at least one more element to provide; *
false 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>的源代码和翻译之后,想必您和我都有同一个想法:这玩意并没有什么*用。

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

 

转载于:https://www.cnblogs.com/forget406/p/6064192.html

你可能感兴趣的文章
洛谷 P1443 马的遍历
查看>>
Asp.net MVC3 中,动态添加filter
查看>>
人民币主动贬值 你的理财方式主动调整了吗?加上外币兑换的费用,也远不如投资人民币理财产品带来的收益高...
查看>>
m6-第11周作业
查看>>
JMeter 功能挖掘之 WEB 文件导出
查看>>
Java中线程池的介绍
查看>>
js之滚动置顶效果
查看>>
algorithms第四版学习进程(一)背包,栈,队列
查看>>
HTML(五)选择器--伪类选择器
查看>>
Postgresql pg_dump
查看>>
ebtables
查看>>
Bulk Load-HBase数据导入最佳实践
查看>>
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
查看>>
ResultSet转成java类对象
查看>>
拟人拟物法求解不等圆Packing问题
查看>>
斐波那契堆
查看>>
HTML5 DragAPI
查看>>
VC设置程序热键
查看>>
LeetCode-96-Unique Binary Search Trees
查看>>
C++实现多线程类Thread
查看>>