Source of AList.java


  1: import java.util.Arrays;
  2: /**
  3:    A class that implements a list of objects by using an 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 5.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 integrityOK;
 15:         private static final int DEFAULT_CAPACITY = 25;
 16:         private static final int MAX_CAPACITY = 10000;
 17:    
 18:         public AList()
 19:         {
 20:                 this(DEFAULT_CAPACITY);
 21:         } // end default constructor
 22:    
 23:         public AList(int initialCapacity)
 24:         {
 25:       integrityOK = false;
 26:       
 27:       // Is initialCapacity too small?
 28:       if (initialCapacity < DEFAULT_CAPACITY)
 29:          initialCapacity = DEFAULT_CAPACITY;
 30:       else // Is initialCapacity too big?
 31:          checkCapacity(initialCapacity);
 32:       
 33:       // The cast is safe because the new array contains null entries
 34:       @SuppressWarnings("unchecked")
 35:       T[] tempList = (T[])new Object[initialCapacity + 1];
 36:       list = tempList;
 37:       numberOfEntries = 0;
 38:       integrityOK = true;
 39:         } // end constructor
 40:    
 41:         public void add(T newEntry)
 42:         {
 43:        checkIntegrity();
 44:        list[numberOfEntries + 1] = newEntry;
 45:        numberOfEntries++;
 46:        ensureCapacity();
 47: //     add(numberOfEntries + 1, newEntry);  // ALTERNATE CODE
 48:         } // end add

 50:         public void add(int newPosition, T newEntry)
 51:         {  /* < Implementation deferred > */
 52:         } // end add

 54:         public T remove(int givenPosition)
 55:         { /* < Implementation deferred > */
 56:         } // end remove

 58:         public void clear()
 59:         { /* < Implementation deferred > */
 60:         } // end clear

 62:         public T replace(int givenPosition, T newEntry)
 63:         { /* < Implementation deferred > */
 64:         } // end replace

 66:         public T getEntry(int givenPosition) 
 67:         { /* < Implementation deferred > */
 68:         } // end getEntry

 70:    public T[] toArray()
 71:    {
 72:                 checkIntegrity();
 73:       
 74:       // The cast is safe because the new array contains null entries
 75:       @SuppressWarnings("unchecked")
 76:       T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
 77:       for (int index = 0; index < numberOfEntries; index++)
 78:       {
 79:          result[index] = list[index + 1];
 80:       } // end for
 81:       
 82:       return result;
 83:    } // end toArray
 84:    
 85:         public boolean contains(T anEntry)
 86:         { /* < Implementation deferred > */
 87:         } // end contains

 89:         public int getLength()
 90:         {
 91:                 return numberOfEntries;
 92:         } // end getLength

 94:         public boolean isEmpty()
 95:         {
 96:                 return numberOfEntries == 0; // Or getLength() == 0
 97:         } // end isEmpty

 99:    // Doubles the capacity of the array list if it is full.
100:    // Precondition: checkIntegrity has been called.
101:    private void ensureCapacity()
102:    {
103:       int capacity = list.length - 1;
104:       if (numberOfEntries >= capacity)
105:       {
106:          int newCapacity = 2 * capacity;
107:          checkCapacity(newCapacity); // Is capacity too big?
108:          list = Arrays.copyOf(list, newCapacity + 1);
109:       } // end if
110:    } // end ensureCapacity

112: /* < This class will define checkCapacity, checkIntegrity, and two more private methods that will be discussed later. > */
113: } // end AList