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).
Diese Übung ist Teil des Kurses
Data Types and Exceptions in Java
Anleitung zur Übung
- Import
HashSet
for use in the application. - Construct a new
HashSet
ofInteger
s and set theheights
variable to it. - Add a new height,
64
, to theheights
list. - Replace
64
withnull
.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
// Import HashSet
____ ____.____.____;
public class Sizes {
public static void main(String[] args) {
// Create an HashSet of Integer using parameterized constructor
____<____> heights = ____ ____<____>();
heights.add(72);
// Add 64 to the set
heights.____(____);
heights.add(66);
heights.remove(64);
// Add null to the set
____.____(____);
System.out.println(heights);
}
}