使用 HashSet
创建一个用于保存身高(英寸,Integer)的 HashSet,添加一些新的身高,并把其中一个身高替换为 null(以验证 Set 可以存放 null 对象)。
本练习是课程的一部分
Java 的数据类型与异常
练习说明
- 导入要在应用中使用的
HashSet。 - 构造一个新的
Integer类型的HashSet,并将变量heights指向它。 - 向
heights列表添加一个新的身高64。 - 将
64替换为null。
交互式实操练习
通过完成这段示例代码来试试这个练习。
// Import HashSet
mport java.____.____;
public class Sizes {
public static void main(String[] args) {
// Create a HashSet of Integer using parameterized constructor
____<____> heights = new ____<____>();
heights.add(72);
// Add 64 to the set
heights.____(____);
heights.add(66);
heights.remove(64);
// Add null to the set
heights.____(____);
System.out.println(heights);
}
}