Java Enumeration(列舉)介面
阿新 • • 發佈:2021-11-06
列舉(The Enumeration)介面定義了一種從資料結構中取回連續元素的方式。
Java8中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 static void main(String[] args) { Enumeration<String> enumeration; Vector<String> vc=new Vector<>(); vc.add("name"); vc.add("sex"); vc.add("address"); enumeration=vc.elements(); while (enumeration.hasMoreElements()){ System.out.println(enumeration.nextElement()); }
輸出結果:
name
sex
address