透過 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
廣告