JAVA 的Enumeration(列举)

Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法。   

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.     

 */ 

 

boolean hasMoreElements();

/**      

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

*/     

E nextElement();   

}

 public interface Enumeration<E>  实现Enumeration接口的对象,它生成一系列元素,一次生成一个。

Enumeration有两个方法:

hasMoreElements()   测试此枚举是否包含更多的元素。

nextElement()           如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。连续调用 nextElement 方法将返回一系列的连续元素。

例如,要输出 Vector<E> v 的所有元素,可使用以下方法:

for (Enumeration<E> e = v.elements(); e.hasMoreElements();)

System.out.println(e.nextElement());

这些方法主要通过向量的元素、哈希表的键以及哈希表中的值进行枚举。枚举也用于将输入流指定到 SequenceInputStream 中。

注:此接口的功能与 Iterator 接口的功能是重复的。

此外,Iterator 接口添加了一个可选的移除操作,并使用较短的方法名。新的实现应该优先考虑使用 Iterator 接口而不是 Enumeration 接口。

posted @ 2015-09-23 17:01  @猪头@  阅读(559)  评论(0编辑  收藏  举报