If we kept the items in the list ordered instead, we could use binary search when checking for items, which would be much faster. That means we'd need to be careful when adding new items, to ensure that the list stayed ordered, but binary search can help us here too! As described below, binary search can get us to the position at which a new item needs to be inserted to keep the list ordered. Your job will be to fill in the missing methods to create a set for holding integer values.
private ArrayList<Integer> values;When the user asks us to add a value to a set, we will store it in this ArrayList, but we'll be careful to make sure the list stays ordered when adding new items so that we can use binary search on it when necessary. The steps below will guide you through implementing the methods required for the set class.
find
method that can be used as a "helper" elsewhere: It should take an int
as its input and use binary search to narrow the search down to a single location in the values
ArrayList, but instead of checking whether the "key" really is at that location it should return the index of that final "box".
You'll need to adapt our binary search code from class so that it works on ArrayLists, but the required changes should be pretty minimal. Also, while you might want to leave this method public
until you've finished testing your code, it should be private
in the final version you submit. This is not a method we want the user to access — it's only there to assist in the implementation of other methods within the class. Also, for full credit, make sure you use your find
method in the rest of your code any time you need to search the ArrayList. (Do not use methods like contains
or indexOf
on the values
list elsewhere in your class.)
toString
method for the IntSet class that returns a string containing all of the integer values stored in the set. It's fine if this string looks exactly like the results of ArrayList's toString
method. (That was a hint.)
size
method that returns the number of integers in the IntSet. (If this is more than one line of code, you made it harder than it needed to be.)
contains
that takes an integer as its input and returns true if the item is in the set (false if it's not). Since you already implemented binary search in the find
method, there's not much to do here: Call find to see where the item would be located if it were in the ArrayList, then check that one position to see if it's there.
add
that takes an integer as its input, and adds it to the set if it's not already present. This requires a bit of care, since we need to keep the list ordered. Here's a start at a to-do list:
find
to figure out where it should be and...
<
what's there, add at position find returned
>
what's there, add it to the right of that position
find
as described above to add the value in the right spot since the list is already in order before the call to add
.
merge
that takes an IntSet
as its input, and adds all of the items from the input set to the current set. (Hint: You just need to call the add
method from the previous step a bunch of times — once for each item in the input set.)
remove
that takes an integer as its input and removes that value from the set if it was present. The find
method is your friend here as well:
find
to see where the item would be
subtract
that takes an IntSet
as its input, and removes all items in that set from our current set. Just like merge
, this just means calling remove
once for each item in the input set.
IntSet
are shown below. Note that my find
method was left public for these interactions so I could test it, but I later changed it to private as required. Also, note that unless find
works, none of the other methods will work since they all depend on the list staying in order!
IntSet s = new IntSet(); s.add(20); s.add(10); s.add(30); s.add(5); s.add(30); s.toString() "[5, 10, 20, 30]" (String) s.size() 4 (int) s.find(5) 0 (int) s.find(4) 0 (int) s.find(25) 2 (int) s.find(35) 3 (int) s.contains(5) true (boolean) s.contains(30) true (boolean) s.contains(15) false (boolean) s.remove(15); s.remove(20); s.remove(5); s.toString() "[10, 30]" (String)