/* Peter Huitsing Instuctor: Mr. Saha Lab2 - Array-based implementation of lists Lab section 2 */ // ******************************************************** // Array-based implementation of the ADT list. // ********************************************************* public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int numItems; // number of items in list public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor public ListArrayBased(Object item) { items = new Object[MAX_LIST]; items[0]=item; numItems = 1; } // end constructor public ListArrayBased(ListArrayBased L) { numItems = L.numItems; items = new Object[MAX_LIST]; for (int i = 0; i < MAX_LIST; i++) items[i]=L.items[i]; }// end of copy constructor public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size public void removeAll() { // Creates a new array; marks old array for // garbage collection. //System.out.println("Deleting the entire list"); items = new Object[MAX_LIST]; numItems = 0; } // end removeAll public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (numItems > MAX_LIST) { throw new ListException("ListException on add"); } // end if if (index >= 1 && index <= numItems+1) { // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) //System.out.println("Adding list item " + item + " at position "+ index); for (int pos = numItems; pos >= index; pos--) { items[translate(pos+1)] = items[translate(pos)]; } // end for // insert new item items[translate(index)] = item; numItems++; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add"); } // end if } //end add public void append(Object item) { if(numItems= 1 && index <= numItems) { return items[translate(index)]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get public int contains(Object item) {// If item is contained in the current list, return its index // otherwise return -1 int returnMe = -1; for (int i = 0; i= 1 && index <= numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) //System.out.println("Deleting list item at position "+ index); for (int pos = index+1; pos <= size(); pos++) { items[translate(pos-1)] = items[translate(pos)]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if } //end remove public void delete(Object item) {// delete item from the list if it exists (specify position) // otherwise state that the item is not in the current list boolean itemWasNotDeleted = true; for (int i=0; i