Source of replace.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: template<class ItemType>
  5: ItemType ArrayList<ItemType>::replace(int position, const ItemType& newEntry)
  6:                               throw(PrecondViolatedExcept)
  7: {
  8:    // Enforce precondition
  9:    bool ableToSet = (position >= 1) && (position <= itemCount);
 10:    if (ableToSet)
 11:    {
 12:       ItemType oldEntry = items[position];
 13:       items[position] = newEntry;
 14:       return oldEntry;
 15:    }
 16:    else
 17:    {
 18:       std::string message = "replace() called with an empty list or ";
 19:       message = message + "invalid position.";
 20:       throw(PrecondViolatedExcept(message));
 21:    } // end if
 22: }  // end replace