Skip to content

Latest commit

 

History

History
20 lines (13 loc) · 800 Bytes

8a61.md

File metadata and controls

20 lines (13 loc) · 800 Bytes

Back to questions

Solution to 8a61: Int set

See code at solutions/code/tutorialquestions/question8a61

You will see that I decided to implement MemoryEfficientIntSet using an ArrayList, and SpeedEfficientIntSet using a HashSet.

In the implementation of readIntegers(int n) in Demo.java, the key observation is that we can write:

IntSet result = (n > 10 ? new MemoryEfficientIntSet() : new SpeedEfficientIntSet());

to create either a MemoryEfficientIntSet or a SpeedEfficientIntSet depending on n. Whichever kind of set is created, the resulting reference is stored in result which has type IntSet. This means that thereafter we can manipulate the set through the IntSet interface regardless of its actual type.