previous | start | next

Using an Abstract Data Type - SetOfInts

The client code above is not too complicated, but even in this case, can be made clearer and less dependent on the implementation (i.e., the data representation and/or the search implementation).

USe a SetOfInts class that stores the data from an array of keys and provides a public contains method.

public class SetOfInts
{
  private int[] keys;

  public SetOfInts(int[] a)
  {
    keys = a;
    Arrays.sort(keys);
  }

  private int rank(int item)
  {
    // binary search version of rank
    ...
  }
  
  public boolean contains(item)
  {
    return rank(item) != -1;
  }
}


previous | start | next