Enumeration

public interface Enumeration {
    /**
     * Tests if this enumeration contains more elements.
     *
     * @return  true if and only if this enumeration object
     *           contains at least one more element to provide;
     *          false 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();
}

简单应用:
package com;

import java.util.Enumeration;
import java.util.Vector;

public class EnumerationPractice {
	
	public static void main(String[] args) {
		Vector vector = new Vector<>();
		vector.addElement("obj01");
		vector.addElement("obj02");
		vector.addElement("obj03");
		vector.addElement("obj04");
		vector.addElement("obj05");
		
		boolean bool = vector.isEmpty();
		System.out.println("vector is empty? " + bool);
		
		vector.add(3, "obj03.5");
		
		Enumeration elements = vector.elements();
		
		vector.setElementAt("obj_new", 5);
		
		while(elements.hasMoreElements()){ //判断是否还有元素,如果有返回true
			String element = elements.nextElement(); //获取下一个元素
			System.out.println("element:" + element);
		}
		System.out.println();
	}
}
结果如下:
vector is empty? false
element:obj01
element:obj02
element:obj03
element:obj03.5
element:obj04
element:obj_new


你可能感兴趣的:(java)