Java 資料結構 - 建立集合
java.util包中的Set介面表示Java中的集合。HashSet和LinkedHashSet類實現了此介面。
要建立一個集合(物件),您需要例項化這兩個類中的任何一個。
Set set = new HashSet();
示例
import java.util.HashSet;
import java.util.Set;
public class CreatingSet {
public static void main(String args[]) {
Set set = new HashSet();
set.add(100);
set.add(501);
set.add(302);
set.add(420);
System.out.println("Contents of the set are: "+set);
}
}
輸出
Contents of the set are: [100, 420, 501, 302]
廣告