15-200 Lecture 4 (Wednesday, September 6, 2006)

Today's Example

Today, we're going to create a generic Container based on an array of objects. The idea is that we'll be able to add and remove things from this Container, as well as to determine if an Object is already within the container.

The example serves to illustrate the use of arrays -- both their syntax and sematics, and also how they can be used in practice.

The Storage

Storage within the Container is based on an array of Objects. We choose to create an array of Objects, becuase it enables this Container to hold anything, because all objects are derived from Object and all primitives can be wrapped within Objects (more on that soon).

We initialize the array when the Container is created, via the constructor. But, how big should it be? We'll answer that question two ways:

...this way, we can accomodate the expected common cases -- and also much larger cases.

We also keep an instance variable, nextSlot, that serves to tell us the next available slot in the array -- in other words, where we put the next item. Since the array's first index is 0, this variable also contains the count of items -- it is initially 0 and remains one ahead of the most recently added item.

In creating the class, we'll use a "final" variable to set the default size. Since this is a definition, not a property of an instance, we make it "static". This type of variable is known as a "configuration constant". This is because it can be changed, a.k.a., configured, but only before the program is compiled, hence "constant" during the program's execution.

class Containter {

  private Object[] list;
  private int nextSlot;
  
  private static final int DEFAULT_SIZE = 10;
  

  public Container() {
    list = new Object[DEFAULT_SIZE];
    nextSlot = 0;
  }
  
  
  public Container (int size) {
    list = new Object[size];
    nextSlot = 0;
  }

}

toString()

At this point, we can write toString(). Doing so will enable us to test, our constructors, at least in a trivial way, and also enable us to test everything else we write.

The toString() will return a String containing each item within the Container, one per line. Instead of using a "\n" to represent a newline, I'll ask the System for the new line string. I do this becuase different environments can choose different Strings. For example, UNIX systems use only a "\n", literally "newline", whereas Windows systems use "\r\n", literally "carriage return, new line".

Notice I'm adding another configuration constant at the top. Notice the use of the System.getproperty() method:

Beyond the small detail, the overall strategy is a simply traversal of the array. We walk through each item from the 0th through the last one, the one before nextSlot, and add each to the String that we'll eventually return. After we add each, we add a new line.

  private static final int DEFAULT_SIZE = 10;
  private static final String NL = System.getProperty ("line_separator");

  ...

  public String toString() {
  
    String rep = "";
    
    for (int index=0; index < nextSlot; index++) {
      rep += items[index] + NL;
    }
  }

The add(...) Method

The add method is, in principle, very straight-forward. We have our state variable, nextSlot, that maintains the position of the next available slot. We drop our item off into this slot and then advance the nextSlot counter.

About the only thing that can go wrong is that the array is already full. If the array is already full, (nextSlot == list.length). This is because an array of length list.length has indexes [0...(list.length-1)] -- but not list.length. Should we try to access an element outside of the legal bounds, an ArrayIndexOutOfBoundsException will be thrown. This is an "unreported" exception, so it doesn't need to be "declared thrown", but it certainly can and will arise.

Next class, we'll learn to make our array's grow in response to demand. But, for today's class, we'll just take advantage of this Exception and return false, should it occur.

Notice that after adding an item, we bump nextSlot forward -- this gets us ready to to it again for the next one.

  public boolean add (Object item) {
 
   try {
     list[nextSlot++] = item;
     return true;
   } 
   catch (ArrayIndexOutOfBoundsException aioobe) {
     return false;
   }
  }

The contains(...) Method

The contains(...) method is really an extension of the same traveral we used for toString(). We walk through each item within the Collection, from [0...(.length-1)]. The only difference is that, for each item, we check to see if it matches the one for which we are searching using equals(). If it does, we immediately return true -- we are done, there is no reason to continue the traversal. If however we work our way through the entire array and don't find a match, we return false -- there is no where else to look.

  public boolean contains (Object item) {
  
    for (int index=0; index < nextSlot; index++) {
      if (items[index].equals(item)) 
        return true;
    }
    
    return false;
  
  }

Container, so far

class Containter {

  private Object[] list;
  private int nextSlot;
  
  private static final int DEFAULT_SIZE = 10;
  private static final String NL = System.getProperty ("line_separator");
  

  public Container() {
    list = new Object[DEFAULT_SIZE];
    nextSlot = 0;
  }
  
  
  public Container (int size) {
    list = new Object[size];
    nextSlot = 0;
  }
  

  public boolean add (Object item) {
 
   try {
     list[nextSlot++] = item;
     return true;
   } 
   catch (ArrayIndexOutOfBoundsException aioobe) {
     return false;
   }
  }
  
  
  public boolean contains (Object item) {
  
    for (int index=0; index < nextSlot; index++) {
      if (items[index].equals(item)) 
        return true;
    }
    
    return false;
  
  }
  
  public String toString() {
  
    String rep = "";
    
    for (int index=0; index < nextSlot; index++) {
      rep += items[index] + NL;
    }
  }

}

The Next Challenge

Okay. As of right now, if the underlying array is full, we return false. While that prevents a disaster, it isn't exactly agile. So, we're going to fix that by making our Container grow to accomodate more items, as needed. Now, we can't actually grow an array. And, managing multiple arrays would make a simple solution into a complex nightmare. So, we're going to have to solve this problem by slight of hand.

The Strategy

Fortunately, that's not as hard as it seems. Remember that "list" is not an array -- it is a reference variable. It identifies an array. The user accesses the array only indirectly through it. This is called "indirection". And, the great thing about it is that the user doesn't need to know which array object is being used -- just that it is referenced by "list".

This makes it possible for us to play the shell game and switch the array underneath list from the small, filled array to a new bigger one. And, the user of list will never know the difference.

So, the solution looks like this:

  1. We create a new,bigger array
  2. We copy each reference from the old array to the new array, keeping it at the correpsonding index. Item 0 to item 0, item 1 to item 1, &c.
  3. We change "list" to reference the new array instead of the old array
  4. The garbage collector is now free to take care of the unreferenced old array.
  5. The user can't tell the difference -- they are still using list and everything is still at the same position.

So, How Much Bigger?

So, our basic approach will be to create a new, bigger array to replace the old one. But, how much bigger? In a technical sense, we solve the problem if we make it only one slot larger -- then our insert can succeed.

But the problem is that we'll end up growing the array each time -- and that ain't cheap. Consider an array with 1,000,000,000 items. It it full, so we create one with 1,000,000,001 items. This involves copying the references for each fo the original 1,000,000,000 items. Now, we add one more, so we copy 1,000,000,001 items. And, each time we insert, we'll end up doing the same thing.

Ouch! So, we clearly want to grow by more than one. Typically these data structures grow by doubling. This way, there is plenty of room to grow. In 15-211 you'll learn that this approach leads to a reasonable average cost, known as :"amortized constant time". For now, we can just use our intuition and see that we end up copying a lot less this way. We're trading space for time -- a classic trade.

But, since this trade might be made differently if we knew that the array was large and wouldn't grow very often versus if it were small and dynamic, we make this growth factor a "configuration constant". A constant that can be changed in one place at compile time.

The grow() Method

The completed grow() method is below:

  private void grow() {

    Object[] biggerList = new Object[(int)(GROWTH_FACTOR * list.length)];

    for (int index=0; index < nextSlot; index++)
      biggerList[index] = list[index];

    list = biggerList;
  }

Using grow()

Now that we have the grow() method, how do we use it? Simple. In each case where we would otherwise have returned false because the array is full, we simply call grow() and continue. After grow(), the array will have plenty of space. Since there is no possibility of failure, these methods can no be "void".

Please consider add(), below, as an example:

  public void add (Object item) {

    if (nextSlot == list.length)
      grow();

    list[nextSlot++] = item;
  }

Container, so far


class Container {

  private Object[] list;
  private int nextSlot;
  
  private static final int DEFAULT_SIZE = 10;
  private static final double GROWTH_FACTOR = 2.0;
  private static final String NL = System.getProperty ("line_separator");
  

  public Container() {
    list = new Object[DEFAULT_SIZE];
    nextSlot = 0;
  }
  
  
  public Container (int size) {
    list = new Object[size];
    nextSlot = 0;
  }
  
  
  // Add an "insertFirst" method to this container.
  // It should add the new item at index 0
  // Note: Yes, it does need to shift the rest back 
  public boolean insertFirst (Object item) {
  
    if (nextSlot == list.length) 
      grow();
      
    for (int index=nextSlot; index > 0; index--) {
      list[index] = list[index-1];
    }
    
    list[0] = item;
    nextSlot++;
  }
  
  

  public void add (Object item) {
 
    if (nextSlot == list.length)
      grow();
  
    list[nextSlot++] = item;
  }
  
  
  public boolean contains (Object item) {
  
    for (int index=0; index < nextSlot; index++) {
      if (items[index].equals(item)) 
        return true;
    }
    
    return false;
  
  }
  
  public boolean remove (Object item) {
  
    int index;
  
    for (index=0; index < nextSlot; index++) {
    
      if (list[index].equals(item))
        break;  
    }
    
    if (index == nextSlot)
      return false;
    
    for (int hole=index; hole < (nextSlot-1); hole++) {
      list[hole] = list[hole+1];
      list[hole+1] = null;
    }
    
    nextSlot--;
  
  }
  
  
  private void grow() {
  
    Object[] biggerList = new Object[(int)(GROWTH_FACTOR * list.length)];
    
    for (int index=0; index < nextSlot; index++) 
      biggerList[index] = list[index];
      
    
    list = biggerList;
  
  
  }
  
  public String toString() {
  
    String rep = "";
    
    for (int index=0; index < nextSlot; index++) {
      rep += items[index] + NL;
    }
  }
}