package studentDB; /** * Maintains a list of objects. Objects may be entered * into the list and retrieved from the list. * * Updated version implemented as array. */ public class ListImpl { // The list consists of elements[0],..., elements [lastElement]. // The list is defined to have maxElements elements, running // from 0..maxElements-1. private int lastElement; // Note that the constructor below does not provide maxElements, // so we set to a default value of 100. private int maxElements = 100; private Object[] elements; /** * Creates an empty list. */ public ListImpl () { // An empty list has lastElement pointing before the list lastElement = -1; elements = new Object [maxElements]; } /** * Adds the given element at the end of the list. * @param newElement The element being added * @param sentinel An empty list element */ protected void addElement (Object newElement, ListImpl sentinel) { // If there is room in the list, adds the element to the // last position. Note that the sentinel parameter plays no role // in the array implementation, so is ignored. if (isFull()) { System.out.println ("Attempt to add to full list"); } else { lastElement++; elements [lastElement] = newElement; } } /** * Returns the number of elements in the list. * @return Number of elements in the list. */ public int numElements () { return lastElement + 1; } /** * Determines whether given element is in the list. */ public boolean inList (Object element) { for (int i=0; i <= lastElement; i++) { if (elements [i].equals (element)) { return true; } } return false; } /** * Returns the list element at position n. Indexing is * zero-based, so it must hold that 0<=n= 0 && n < numElements(), "getElement out of bounds"); return elements [n]; } /** * Removes the list element at posision n. Indexing is * zero-based, so it must hold that 0<=n= 0 && n < numElements(), "getElement out of bounds"); // Move all elements following n down one position. Ie, // elements [n] = elements [n+1]; // elements [n+1] = elements [n+2]; // etc. for (int i=n; i