向 Set 中添加元素
您可以使用 Set 接口的 add() 方法向集合中添加元素。此方法接受一个元素作为参数,并将给定元素/对象追加到集合中。
示例
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]
广告