回归JDK源代码(2)Enumeration<E>接口

现在的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>的源代码和翻译之后,想必您和我都有同一个想法:这玩意并没有什么*用。

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

 

posted @ 2016-11-15 02:55  forget406  阅读(756)  评论(0编辑  收藏  举报