Source of 11.15.java


  1: // Doubles the size of the array queue if it is full
  2: // Precondition: checkInitialization has been called.
  3: private void ensureCapacity()
  4: {
  5:    if (frontIndex == ((backIndex + 2) % queue.length)) // if array is full,
  6:    {                                                   // double size of array
  7:       T[] oldQueue = queue;
  8:       int oldSize = oldQueue.length;
  9:       int newSize = 2 * oldSize;
 10:       checkCapacity(newSize);
 11: 
 12:       // The cast is safe because the new array contains null entries
 13:       @SuppressWarnings("unchecked")
 14:       T[] tempQueue = (T[]) new Object[2 * oldSize];
 15:       queue = tempQueue;
 16:       for (int index = 0; index < oldSize - 1; index++)
 17:       {
 18:          queue[index] = oldQueue[frontIndex];
 19:          frontIndex = (frontIndex + 1) % oldSize;
 20:       } // end for
 21:       
 22:       frontIndex = 0;
 23:       backIndex = oldSize - 2;
 24:    } // end if
 25: } // end ensureCapacity
 26: // Version 4.0