透過 Java 獲取 HashSet 上的列舉


要透過 HashSet 獲取列舉,首先宣告 HashSet 並新增元素 -

HashSet<String> hs = new HashSet<String>();
hs.add("P");
hs.add("Q");
hs.add("R");

要獲取列舉 -

Enumeration e = Collections.enumeration(hs);

以下是一個透過 HashSet 獲取列舉的示例 -

示例

 線上演示

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      HashSet<String> hs = new HashSet<String>();
      hs.add("P");
      hs.add("Q");
      hs.add("R");
      Enumeration e = Collections.enumeration(hs);
      while (e.hasMoreElements())
      System.out.println(e.nextElement());
   }
}

輸出

P
Q
R

更新於: 30-7-2019

213 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告