Source of 11.15.java


  1: // @author Frank M. Carrano, Timothy M. Henry
  2: // @version 5.0
  3: public void add(int givenPosition, T newEntry)
  4: {
  5:    checkIntegrity();
  6:    if ((givenPosition >= 1) && (givenPosition <= numberOfEntries + 1))
  7:    {
  8:       if (givenPosition <= numberOfEntries)
  9:          makeRoom(givenPosition);
 10:       list[givenPosition] = newEntry;
 11:       numberOfEntries++;
 12:       ensureCapacity(); // Ensure enough room for next add
 13:    }
 14:    else
 15:       throw new IndexOutOfBoundsException(
 16:                 "Given position of add's new entry is out of bounds.");
 17: } // end add

 19: private void makeRoom(int givenPosition)
 20: {
 21:    int newIndex = givenPosition;
 22:    int lastIndex = numberOfEntries;
 23:    for (int index = lastIndex; index >= newIndex; index--)
 24:       list[index + 1] = list[index];
 25: }  // end makeRoom