// CSE 143, Summer 2012 // An ArrayList object stores an ordered list of objects using // an unfilled array. // This version uses "generics" so that it can store any type of value, not // just integers. It also implements the same interface as LinkedList, // so that the two can be used interchangeably in client code. import java.util.Arrays; import java.util.Iterator; public class ArrayList extends AbstractList { public static final int DEFAULT_CAPACITY = 10; private E[] elementData; private int size; // initializes a new empty list with initial capacity of 10 integers. public ArrayList() { this(DEFAULT_CAPACITY); } // initializes a new empty list with the given initial capacity. // pre: capacity > 0 public ArrayList(int capacity) { if (capacity <= 0) { throw new IllegalArgumentException("capacity must be positive: " + capacity); } elementData = (E[]) new Object [capacity]; size = 0; } // inserts the given value into the list at the given index. // resize if necessary // pre: 0 <= index <= size (can add at very end) public void add(int index, E value) { checkIndex(index, 0, size); if (size == elementData.length) { elementData = Arrays.copyOf(elementData, size * 2); } // shifting over elementData to make room for the new value for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // removes the value from the given index, shifting following elements left // by 1 slot to cover the hole. // pre: 0 <= index < size public void remove(int index) { checkIndex(index, 0, size - 1); for (int i = index; i <= size - 2; i++) { elementData[i] = elementData[i + 1]; } elementData[size - 1] = null; size--; } // returns the value in the list at the given index. // pre: 0 <= index < size public E get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // sets the given index to store the given value. // pre: 0 <= index < size public void set(int index, E value) { checkIndex(index, 0, size - 1); elementData[index] = value; } // returns the number of elements in the list. public int size() { return size; } // returns the first index an element is found, or -1 if not public int indexOf(E value) { for (int i = 0; i < size ; i++) { if (get(i).equals(value)) { return i; } } return -1; } // returns a comma-separated, bracketed representation of the list public String toString() { String result = "["; if (!isEmpty()) { result += elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } } result += "]"; return result; } // if index is not within bounds, throw exception private void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new ArrayIndexOutOfBoundsException("Invalid index " + index); } } public Iterator iterator() { return new ArrayListIterator(); } // This inner class implements an Iterator over the elements of this list. // Not perfect; doesn't forbid multiple removes in a row. private class ArrayListIterator implements Iterator { private int index; // current position in list public ArrayListIterator() { index = 0; } public boolean hasNext() { return index < size(); } public E next() { index++; return get(index - 1); } public void remove() { ArrayList.this.remove(index - 1); index--; } } }