二话不说,来看官方文档:

public interface Enumeration<E>
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.
实现了枚举接口的对象会生成一系列元素,一次一个。通过连续的调用nextElement方法获得连续的元素。

拿vector的elements方法源码举例:

public Enumeration<E> elements() {
//通过匿名类方式实现了Enumeration接口
return new Enumeration<E>() {
int count = 0; public boolean hasMoreElements() {
return count < elementCount;
} public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData(count++);
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}

For example, to print all elements of a Vector<E> v:



   for (Enumeration<E> 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.



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.

说明:本接口功能已被Iterator接口取代。Iterator接口扩展了删除方法,并且具有更简洁的方法名。

再来写个实例,加深了解:

package com.dylan.collection;

import java.util.Enumeration;
import java.util.Vector; /**
* 测试枚举接口,
* 可用于遍历集合类型,目前已被迭代器Iterator取代
*
* @author xusucheng
* @create 2017-12-25
**/
public class EnumerationTest {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Jack");
v.add("ate");
v.add("lots of oranges.");
Enumeration<String> e = v.elements();
String output = "";
while (e.hasMoreElements()) {
output += e.nextElement() + " ";
} System.out.println(output);
}
}

Java Enumeration接口详解的更多相关文章

  1. Java修炼——接口详解_接口的特征_使用接口的意义

    接口中可以包含的内容: 抽象法和非抽象方法(jdk1.8,必须使用default的关键字),属性(public static final)常量. 接口和类的关系 1.(继承了接口)类必须去实现接口中的 ...

  2. “全栈2019”Java第八十四章:接口中嵌套接口详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. “全栈2019”Java第八十三章:内部类与接口详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. Java接口 详解(二)

    上一篇Java接口 详解(一)讲到了接口的基本概念.接口的使用和接口的实际应用(标准定义).我们接着来讲. 一.接口的应用—工厂设计模式(Factory) 我们先看一个范例: package com. ...

  5. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  6. Java 序列化Serializable详解

    Java 序列化Serializable详解(附详细例子) Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化Serialization(序列化)是一种将对象以一连 ...

  7. 最新java数组的详解

    java中HashMap详解 http://alex09.iteye.com/blog/539545 总结: 1.就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并不是真正的把 Java ...

  8. 转:Java HashMap实现详解

    Java HashMap实现详解 转:http://beyond99.blog.51cto.com/1469451/429789 1.    HashMap概述:    HashMap是基于哈希表的M ...

  9. 【转】 java中HashMap详解

    原文网址:http://blog.csdn.net/caihaijiang/article/details/6280251 java中HashMap详解 HashMap 和 HashSet 是 Jav ...

  10. JAVA命令参数详解

    JAVA命令参数详解 JAVA命令详解 结构 说明 Java 和 OldJava JIT 选项 另请参阅 结构 java [ options ] class [ argument ... ] java ...

随机推荐

  1. Linux-日期时间-date

  2. IBM java的分析工具(ga和ha)学习和整理

    IBM java的分析工具(ga和ha)学习和整理 背景 前几天学习了整理了 jca 工具 今天继续学习一下 ga工具 ga 工具主要是分析gclog相关. 可以很直观的进行gclog的分析和展示. ...

  3. [转帖]不同CPU性能大PK

    https://plantegg.github.io/2022/01/13/%E4%B8%8D%E5%90%8CCPU%E6%80%A7%E8%83%BD%E5%A4%A7PK/ 前言 比较Hygon ...

  4. [转帖]计算机体系结构-cache高速缓存

    https://zhuanlan.zhihu.com/p/482651908 本文主要介绍了cache的基本常识.基本组成方式.写入方法和替换策略,在基本组成方式和替换策略两节给出了较为详细的硬件实现 ...

  5. TypeScript 类型增强declare的使用

    类型增强 declare 的使用 1.如果一个有一个全局变量 golabaol . 在index.html中. 2.我们在xx.vue中使用 golabaol .这个时候会报错 找不到名称" ...

  6. element-ui表格排序

    <el-table :data="TableAwitDoArr" style="width: 100%"> <el-table-column ...

  7. chaincode中使用第三方库

    本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处. 在fabric的chaincode开发时,有时候需要用到第三方库提供的功能.这 ...

  8. 微信小程序-页面跳转wxAPI

    官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html wx.navigateTo(O ...

  9. 【二】Latex入门使用、常见指令

    参考链接:https://blog.csdn.net/cocoonyang/article/details/78036326 \documentclass[12pt, a4paper]{article ...

  10. ASP.NET Core必备知识之Autofac

    使用Autofac替换掉微软的DI 本文的项目为ASP.NET Core3.1,传统三层架构 在这就不过多介绍Autofac,直接上代码 Autofac官网:https://autofac.org/ ...