描述
elements() 方法用来返回此向量的组件的枚举。返回的Enumeration对象将在类似索引位置生成此向量中的所有项。
声明
以下是java.util.Vector.elements()方法的声明
public Enumeration elements()
参数
不带任何输入参数
返回值
它返回此向量的组件的枚举。
异常
NA
实例
以下示例显示了java.util.Vector.elements()方法的用法。
package com.tutorialspoint;
import java.util.Vector;
import java.util.Enumeration;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector vec = new Vector(4);
// use add() method to add elements in the vector
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// adding elements into the enumeration
Enumeration e = vec.elements();
// let us print all the elements available in enumeration
System.out.println("Numbers in the enumeration are :- ");
while (e.hasMoreElements()) {
System.out.println("Number = " + e.nextElement());
}
}
}
让我们编译并运行上面的程序,这将产生以下结果。
Numbers in the enumeration are :-
Number = 4
Number = 3
Number = 2
Number = 1