Source of AList.java


  1: import java.util.Arrays;
  2: /**
  3:    A class that implements the ADT list by using a resizable array.
  4:    Entries in a list have positions that begin with 1.
  5:    Duplicate entries are allowed.
  6:    @author Frank M. Carrano
  7:    @author Timothy M. Henry
  8:    @version 4.0
  9: */
 10: public class AList<T> implements ListInterface<T>
 11: {
 12:         private T[] list;   // Array of list entries; ignore list[0]
 13:         private int numberOfEntries;
 14:    private boolean initialized = false;
 15:         private static final int DEFAULT_CAPACITY = 3;
 16:         private static final int MAX_CAPACITY = 15;
 17:    
 18:         public AList()
 19:         {
 20:                 this(DEFAULT_CAPACITY);
 21:         } // end default constructor
 22:    
 23:         public AList(int initialCapacity)
 24:         {
 25:       // Is initialCapacity too small?
 26:       if (initialCapacity < DEFAULT_CAPACITY)
 27:          initialCapacity = DEFAULT_CAPACITY;
 28:       else // Is initialCapacity too big?
 29:          checkCapacity(initialCapacity);
 30:       
 31:       // The cast is safe because the new array contains null entries
 32:       @SuppressWarnings("unchecked")
 33:       T[] tempList = (T[])new Object[initialCapacity + 1];
 34:       list = tempList;
 35:       numberOfEntries = 0;
 36:       initialized = true;
 37:         } // end constructor
 38:    
 39:         public void add(T newEntry)
 40:         {
 41:        checkInitialization();
 42:        list[numberOfEntries + 1] = newEntry;
 43:        numberOfEntries++;
 44:        ensureCapacity();
 45: //     add(numberOfEntries + 1, newEntry);  // ALTERNATE CODE
 46:         } // end add
 47: 
 48:         public void add(int newPosition, T newEntry)
 49:         {  /* < Implementation deferred > */
 50:         } // end add
 51: 
 52:         public T remove(int givenPosition)
 53:         { /* < Implementation deferred > */
 54:         } // end remove
 55: 
 56:         public void clear()
 57:         { /* < Implementation deferred > */
 58:         } // end clear
 59: 
 60:         public T replace(int givenPosition, T newEntry)
 61:         { /* < Implementation deferred > */
 62:         } // end replace
 63: 
 64:         public T getEntry(int givenPosition) 
 65:         { /* < Implementation deferred > */
 66:         } // end getEntry
 67: 
 68:    public T[] toArray()
 69:    {
 70:                 checkInitialization();
 71:       
 72:       // The cast is safe because the new array contains null entries
 73:       @SuppressWarnings("unchecked")
 74:       T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
 75:       for (int index = 0; index < numberOfEntries; index++)
 76:       {
 77:          result[index] = list[index + 1];
 78:       } // end for
 79:       
 80:       return result;
 81:    } // end toArray
 82:    
 83:         public boolean contains(T anEntry)
 84:         { /* < Implementation deferred > */
 85:         } // end contains
 86: 
 87:         public int getLength()
 88:         {
 89:                 return numberOfEntries;
 90:         } // end getLength
 91: 
 92:         public boolean isEmpty()
 93:         {
 94:                 return numberOfEntries == 0; // Or getLength() == 0
 95:         } // end isEmpty
 96: 
 97:    // Doubles the capacity of the array list if it is full.
 98:    // Precondition: checkInitialization has been called.
 99:    private void ensureCapacity()
100:    {
101:       int capacity = list.length - 1;
102:       if (numberOfEntries >= capacity)
103:       {
104:          int newCapacity = 2 * capacity;
105:          checkCapacity(newCapacity); // Is capacity too big?
106:          list = Arrays.copyOf(list, newCapacity + 1);
107:       } // end if
108:    } // end ensureCapacity
109: 
110: /* < This class will define checkCapacity, checkInitialization, and two more private methods that will be discussed later. > */
111: } // end AList