Java 實例 - 使用 Enumeration 遍曆 HashTable
以下實例演示了如何使用 Enumeration 類的 hasMoreElements 和 nextElement 方法來遍曆輸出 HashTable 中的內容:
Main.java 檔
import java.util.Enumeration;
import java.util.Hashtable;
public class Main {
public static void main(String[] args) {
Hashtable ht = new Hashtable();
ht.put("1", "One");
ht.put("2", "Two");
ht.put("3", "Three");
Enumeration e = ht.elements();
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}
}
以上代碼運行輸出結果為:
Three Two One