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).
Toto cvičení je součástí kurzu
Datové typy a výjimky v Javě
Pokyny k cvičení
- Import
HashSetfor use in the application. - Construct a new
HashSetofIntegers and set theheightsvariable to it. - Add a new height,
64, to theheightslist. - Replace
64withnull.
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
// 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);
}
}