Return to lecture notes index
15-100 Lecture 25 (Friday, March 25, 2004)
Sets
So now we are moving onto thinking of arrays as sets. A set is a collection of objects that has no duplicates in it. So let's write a class that keeps an array that's a set. It will contain familiar methods like a constructor that initalizes an array. We can also do a binary seatch on the array because we are keeping it ordered. However we will have to be careful in our add method. We will have to check that the object we want to insert doesn't already exsist in the array. If it already exists in the array then we should exit the add method having done nothing.

Also notice that we are keeping an array of Comparable. Remember that Comparable is an interface (it specifies what minimal methods must be in a class that implements Comparable). This means that our array can store any object that is from a class that implements Comparable...That's alot of different objects!


class Set {
  Comparable[] members; //using an array of Comparable means that any object of a class that 
                        //implements Comparable can be placed in this array
  int count;
  
  private static final int DEFAULT_LENGTH = 10; //reminder: static means this is a variable that
                       //is a constant (its final) that is part of the class specification but 
                       //not part of the objects that this class makes
  
  public Set() [
    members = new Comparable[DEFAULT_LENGTH];
    count = 0;
  }
  
  public Set(int length){
    members = new Comparable[length];
    count = 0;
  }
  
  public void add(Comparable newItem){
    if (count == members.length)
      grow();
    int index; //using index outside of for loop so it needs to be declared here 
               //for index's scope to be correct.  
    for (int index = count; index > 0; index--){
      if (isMember(newItem)) //keeping a set so don't want to allow duplicates, exit method
        return;
        
      int difference = members[index-1].compareTo(c);
      
      if (difference > 0)
        members[index] = members[index-1];
      if (difference < 0)
        break;
    }
    
    members[index] = newItem;
  }
  
  private void grow() {
    Comparable[] biggerArray = new Comparable[2*members.length];
    
    for (int index = 0; index < count; index++){
      biggerArray[index] = members[index];
    }
    
    members = biggerArray;
  }
  
  //returns true if the item already exsists in the array (we can use 
  //this as a helper method for the add.
  //this can be a binary search because we know the array is ordered
  public boolean isMember(Comparable item) {
    int left = 0;
    int right = count-1;
    int pivot = left + (right-left)/2;
    
    while(right >= left) {
      int difference = members[pivot].compareTo(item);
      
      if (difference == 0)
        return true;
      if (difference > 0)
        right = pivot-1;
      if (difference < 0)
       left = pivot +1;
       
      pivot = left + (right-left)/2  
    }
    
    return false;
  }
}
This class is a starting point and we will be adding more methods specific to sets on Monday.