Return to lecture notes index

15-100 Lecture 24 (Monday, November 6, 2006)

Quick Quiz

**GIVEN** an array-based collection similar to the one we've been developing 
in class (remember "items" array and "count"):

1. Write a method:

  // Returns true if inserted, false otherwise
  // Does not damage (do something bad to) the existing list under any   
  // circumstance.
  public boolean insertBefore (Object insertMe, Object beforeMe) {
 
    // Find the "beforeMe" item
    int index;
    for (index=0; index < count; index++)
      if (items[index].equals(beforeMe))
        break;
    
    // If beforeMe isn't within the array -- bail
    if (index == count) return false;
    
    // Make sure we have enough space
    if (count == items.length) 
      grow();
    
    int hole;
    // Move the hole from the end to where we want it.   
    for (hole=count; hole!=index; hole--)
      items[hole] = items[hole-1];
      
    // Drop the new item into the hole
    items[hole] = insertMe;
    
    // increase the count
    count++;
     
  }

Today's Challenge

Our goal today is to explore another type of operation on a container, ordered, or otherwise. We want to filter the container, returning a new container of only those items satisfying some criteria. In the process we want to leave the original container unchanged.

This type of method can be implemented on ether an ordered or unordered container. On your final exam, you'll be doing filtering upon an unordered container. Since we've most recently be developing an OrderedContainer, we'll work with that class today.

Our goal is to return a new OrderedContainer that contains all items from this container that are strictly less than the item passed in. So, we know that our signature must be as follows:

The greaterThan(...) Method

Our goal is to return a new OrderedContainer that contains all items from this container that are strictly greater than the item passed in. So, we know that our signature must be as follows:

public OrderedContainer greaterThan (Comparable item)

We know that we need to return a container -- so we must create one. Now, we're here:

public OrderedContainer greaterThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  ...

  return newContainer;
}

Now, we need to traverse the original OrderedContainer, adding everything that satisifies the criteria to the newContainer:

public OrderedContainer greaterThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=0; index < nextSlot; index++) {
    if (list[index].compareTo(item) > 0)
      newContainer.add (list[index]);
  }
}

The solution above is correct, but not particularly efficient. It skips past the entire beginning just to get to the end. So, why not work from the end toward the beginning? Here's an improved version:

public OrderedContainer greaterThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=(nextSlot-1); index >= 0; index--) {
    if (list[index].compareTo(item) > 0)
      newContainer.add (list[index]);
  }
}

Next Class

...we'll take a look at some other filters.