MulaiMulai sekarang secara gratis

Working with HashSet

Create a HashSet to hold a list of heights in inches (Integer), add some new heights, and replace one of the heights with null (to see that Set allows for null objects).

Latihan ini adalah bagian dari kursus

Data Types and Exceptions in Java

Lihat Kursus

Petunjuk latihan

  • Import HashSet for use in the application.
  • Construct a new HashSet of Integers and set the heights variable to it.
  • Add a new height, 64, to the heights list.
  • Replace 64 with null.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

// 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);
	}
}
Edit dan Jalankan Kode