import java.util.NoSuchElementException; /** * Array-based implementation of an ordered lists of objects.
* CSE 373 demonstration program, Winter 2006 * @author Hal Perkins * @version 1/5/06 */ public class BasicArrayList implements BasicList { // state private Object[] items; // Elements of this list are stored private int nItems; // items[0..nItems-1] private static final int DEFAULT_CAPACITY = 10; // default capacity of new list // constructor /** * Construct a new empty BasicArrayList */ public BasicArrayList() { items = new Object[DEFAULT_CAPACITY]; nItems = 0; } // queries /* return number of elements in this list. specified in interface BasicList */ public int size() { return nItems; } /* return true if this list is empty. specified in interface BasicList */ public boolean isEmpty() { return size() == 0; } /* return true if obj is in this list. specified in interface BasicList */ public boolean contains(Object obj) { return indexOf(obj) != -1; } /* return location of given item or -1. specified in interface BasicList */ public int indexOf(Object obj) { // sequential search for (int i = 0; i < size(); i++) { if (items[i].equals(obj)) { return i; } } return -1; } // internal methods to check arguments // throw IndexOutOfBoundsException if pos is out of range private void checkIndex(int pos) { if (pos < 0 || pos >= size()) { throw new IndexOutOfBoundsException("Invalid BasicArrayList index"); } } // throw IllegalArgumentException if obj is null private void checkArgument(Object obj) { if (obj == null) { throw new IllegalArgumentException("Attempt to add null to BasicArrayList"); } } // set/get methods /* get item at given position. specified in interface BasicList */ public Object get(int pos) throws IndexOutOfBoundsException { checkIndex(pos); return items[pos]; } /* set item at given position. specified in interface BasicList */ public Object set(int pos, Object obj) throws IllegalArgumentException, IndexOutOfBoundsException { checkIndex(pos); checkArgument(obj); Object result = items[pos]; items[pos] = obj; return result; } // basic list manipulation // local function for add methods // be sure that there are at least howMuch empty spaces at the // end of the array, expanding the array if needed. private void ensureExtraCapacity(int howMuch) { if (size() + howMuch <= items.length) { return; } // increase the array size by the max of it's old size (doubling it) // or the requested capacity, if bigger int needed = Math.max(items.length, howMuch); Object[] newItems = new Object[items.length + needed]; for (int k = 0; k < size(); k++) { newItems[k] = items[k]; } items = newItems; } /* remove everything from this list. specified in interface BasicList */ public void clear() { // null out items to avoid unneeded references to objects for (int i = 0; i < size(); i++) { items[i] = null; } // set list to empty nItems = 0; } /* add item at end of list. specified in interface BasicList */ public boolean add(Object item) throws IllegalArgumentException { checkArgument(item); ensureExtraCapacity(1); // add item to end of list items[nItems] = item; nItems++; return true; } /* add item at beginning of list. specified in interface BasicList */ public void addFirst(Object item) throws IllegalArgumentException { if (size() == 0) { add(item); } else { add(0,item); } } /* add obj at given position. specified in interface BasicList */ public void add(int pos, Object item) throws IllegalArgumentException, IndexOutOfBoundsException { checkIndex(pos); checkArgument(item); ensureExtraCapacity(1); // shift items from pos+1 to size()-1 to the right for (int i = size(); i > pos; i--) { items[i] = items[i-1]; } // add item and adjust list size items[pos] = item; nItems++; } /* remove first copy of obj from this list. specified in interface BasicList */ public boolean remove(Object obj) { int pos = indexOf(obj); if (pos != -1) { remove(pos); return true; } else { return false; } } /* return and remove element from this list. specified in interface BasicList */ public Object remove(int pos) throws IndexOutOfBoundsException { checkIndex(pos); Object result = items[pos]; for (int i = pos; i < size()-1; i++) { items[i] = items[i+1]; } nItems--; items[nItems] = null; // null out extra reference at the end return result; } /* remove and return first element in list. specified in interface BasicList */ public Object removeFirst() throws NoSuchElementException { if (isEmpty()) { throw new NoSuchElementException("Trying to remove first item from empty list"); } return remove(0); } /* remove and return last element in list. specified in interface BasicList */ public Object removeLast() throws NoSuchElementException { if (isEmpty()) { throw new NoSuchElementException("Trying to remove last item from empty list"); } return remove(size()-1); } // printable representation /** * Return a string representation of this BasicArrayList * @return the elements in the list separated by commas and surrounded by [] */ public String toString() { String result = "["; if (size() > 0) { result = result + get(0); } for (int i = 1; i < size(); i++) { result = result + "," + get(i); } return result + "]"; } // iteration /* return an iterator for this list. specified in interface BasicList */ public BasicListIterator iterator() { return new BasicArrayListIterator(); } // Iterator class for BasicArrayList private class BasicArrayListIterator implements BasicListIterator { private int nextPos; // position of next item to be returned by this iteration // construct new iterator initialized to return the first item public BasicArrayListIterator() { nextPos = 0; } /* return true if more items. specified in BasicListIterator */ public boolean hasNext() { return nextPos < size(); } /* return next item. specified in BasicListIterator */ public Object next() throws NoSuchElementException { if (!hasNext()) { throw new NoSuchElementException(); } Object result = items[nextPos]; nextPos++; return result; } // unimplemented iterator functions /* specified in BasicListIterator */ public boolean hasPrevious() { throw new UnsupportedOperationException("not implemented"); } /* specified in BasicListIterator */ public Object previous() throws NoSuchElementException { throw new UnsupportedOperationException("not implemented"); } /* specified in BasicListIterator */ public void remove() throws IllegalStateException { throw new UnsupportedOperationException("not implemented"); } } }