Enumeration
Enumeration接口本身不是一个数据结构。但是,对其他数据结构非常重要。
Enumeration接口定义了从一个数据结构得到连续数据的手段。
例如,Enumeration定义了一个名为nextElement的方法,可以用来从含有多个元素的数据结构中得到的下一个元素。 Enumeration接口提供了一套标准的方法,由于Enumeration是一个接口,它的角色局限于为数据结构提供方法协议。
Enumertion接口中仅定义了下面两个方法:
boolean hasMoreElemerts() 测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。 ·
Object nextElement() 如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。
Enumeration是个接口,不是类,再次,这个东西就是为了实现遍历的,现在已经别迭代器Iterator取代了
例1:
MyDataSttuct.java
package cn.run.zuoye_3;
import java.util.Enumeration;
public class MyDataSttuct{
String[] data;
// 构造器
public MyDataSttuct(){
data=new String[4];
data[0] ="zero";
data[1]="one";
data[2] ="two";
data[3]="three";
}
// 返回一个enumeration对象给使用程序
Enumeration getEnum() {
return new MyEnumerator(0,data.length,data);
}
}
MyEnumerator.java
package cn.run.zuoye_3;
import java.util.Enumeration;
class MyEnumerator implements Enumeration
{
int count; // 计数器
int length; //存储的数组的长度
Object[] dataArray; // 存储数据数组的引用
//构造器
public MyEnumerator(int count,int length,Object[] dataArray){
this.count = count;
this.length= length;
this.dataArray=dataArray;
}
public boolean hasMoreElements() {
return (count< length);
}
public Object nextElement() {
return dataArray[count++];
}
}
DemoEnumeration.java
package cn.run.zuoye_3;
import java.util.*;
class DemoEnumeration{
public static void main(String[] args){
//实例化MyDataStruct类型的对象
MyDataSttuct mySataStruct=new MyDataSttuct();
//得到描述myDataStruct类型对象的enumeration对象
Enumeration<MyDataSttuct> myEnumeration =mySataStruct.getEnum();
System.out.println(myEnumeration);
//使用对象循环显示myDataStruct类型的对象中的每一个元素
while (myEnumeration.hasMoreElements())
System.out.println(myEnumeration.nextElement());
}
}
例2:package cn.run.zuoye_3;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
// 获取系统属性文件中的键值对
Properties pro = System.getProperties();
// 把属性文件中的值放入枚举中+泛型
// Enumeration enu = pro.propertyNames();
Enumeration enu = pro.keys();
// 遍历枚举
while (enu.hasMoreElements()) {
// 获取枚举中的键
String keys = (String) enu.nextElement();
// 通过 键 得到对应的 值
String values = (String) pro.get(keys);
System.out.println("系统属性文件中的数据是:【" + keys + "=" + values + "】");
}
}
}
分享到:
相关推荐
USB枚举过程 USB Enumeration
Here,i give a program traversing PCI Device Configuration Space,pl throw them out to me if you have any questions about rationale.
Iterator与Enumeration分别是实现迭代器和枚举器类的接口,下面就带大家来详解Java中的迭代迭代器Iterator与枚举器Enumeration,以及它们之间的区别.
java枚举类型详细讲解,自己撰写的一本电子书,编写得很详细
swift代码-class类和Enumeration枚举的使用
枚举(Enumeration) 位集合(BitSet) 向量(Vector) 栈(Stack) 字典(Dictionary) 哈希表(Hashtable) 属性(Properties) 以上这些类是传统遗留的,在Java2中引入了一种新的框架-集合框架(Collection),我们...
第10章 结构体和共用体——枚举类型C语言程序设计第10章 结构体和共用体枚举标签枚举数据类型枚举(Enumeration)——一一列举应用场合当某些量仅由有限
枚举(Enumeration) 位集合(BitSet) 向量(Vector) 栈(Stack) 字典(Dictionary) 哈希表(Hashtable) 属性(Properties) 以上这些类是传统遗留的,在Java2中引入了一种新的框架-集合框架(Collection),我们...
Network Enumeration .To keep me from implementing such a task over and over again枚举网络资源
如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。所谓“枚举”是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。声明枚举类型用enum开头。例如: enum weekday{sun, mon, tue,...
首先,几个有用的oneliners;... 用于渗透测试和CTFLinux枚举工具 该项目的灵感来自并使用了许多测试。 不像LINENUM, lse尝试gradualy揭露根据其重要性从一个privesc点的信息。 它是什么? 该She
SMILES枚举,向量化和批处理生成 SMILES枚举是写出所有可能的SMILES形式的分子的过程。 这是在基于序列的分子建模之前进行数据增强的有用技术。 您可以在此或了解有关背景的更多信息 笔记 提供了更多更新的软件包...
Access-based Enumeration Windows Server 2003安装包
进程、线程枚举,窗体组件调用例程的功能
枚举(Enumeration) 位集合(BitSet)向量(Vactor)栈(Stack)字典(Dictionary) 哈希(Hashtable)属性(Properties)
一、枚举的概念: C# 枚举(Enum), 枚举类型是用于声明一组命名的常数的基本数据类型(值类型); 二、枚举的定义: 声明enum变量: enum <enum> {enumeration list}; 其中enum_name 指定枚举的类型名称; ...
Windows枚举 Powershell特权升级枚举脚本。 用法 运行快速标准检查。 .\WindowsEnum.ps1 直接来自CMD powershell -nologo -executionpolicy bypass -file WindowsEnum.ps1 扩展检查将搜索配置文件,各种有趣的...
Intel(R)64 Architecture Processor Topology Enumeration