开始使用免费开始使用

使用 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);
	}
}
编辑并运行代码