Javaexamples-collection-enumeration

提供:Dev Guides
移動先:案内検索

Javaの例-列挙を使用してHashTableを表示する

問題の説明

列挙を使用してHashTableの内容を表示する方法は?

溶液

次の例では、EnumerationクラスのhasMoreElementsおよびnestElementメソッドを使用して、HashTableの内容を表示します。

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