// ArrayIntList provides an implementation of the IntList interface backed by // an array. This will provide O(1) performance for methods like get and set // that access a particular element of the array. The underlying array will be // doubled in size if necessary to accommodate add operations. The appending // add has O(1) performance (amortized for doubling when full). // (GY edited from SR) public class SimpleArrayIntList extends SimpleAbstractIntList { private int[] elementData; // list of integers // elementData.length is the capacity of the array, which is different // from the number of elements actually being used (size) private int size; // current number of elements in the list // Pretty much all your instance variables should be private. We'll // make a slight exception for this with the ListNode class, where the // class doesn't have any methods aside from constructors. // public static final is basically a constant. Everyone can see it, there // is only one copy per class, and it cannot change once initialized. // Standard Java convention (people care about this, not the compiler) // is for all constants to be in all CAPS, with _ separating words. public static final int DEFAULT_CAPACITY = 100; // post: constructs an empty list of default capacity public SimpleArrayIntList() { // This convenience constructor invokes the master constructor this(int) this(DEFAULT_CAPACITY); } // pre : capacity >= 0 // post: constructs an empty list with the given capacity // This is the master constructor, with all the possible parameters // necessary. public SimpleArrayIntList(int capacity) { if (capacity < 0) throw new IllegalArgumentException("negative capacity"); // An array is an object, so we create one with new. elementData = new int[capacity]; size = 0; } // post: returns the current number of elements in the list // This seemingly pointless simple method is useful because: // 1) size is now read-only // 2) we can do accounting, such as logging the number of accesses // 3) we can have internal units for the computer and external units // for the user, say in an app using radians inside and degrees outside. public int size() { return size; } // pre : 0 <= index < size() // post: returns the integer at the given index in the list public int get(int index) { checkIndex(index); // the protected method from the abstract class return elementData[index]; } // post: appends the given value to the end of the list public void add(int value) { ensureCapacity(size + 1); // if we don't have enough room, get more elementData[size] = value; // size items means the last // was at bin (size-1) size++; // update size because we added an item } /////////////////////////////////// We stopped here on Sep 30 /////////////////////////////////////////////////////////////////// // pre: 0 <= index <= size() // post: inserts the given value at the given index, shifting subsequent // values right public void add(int index, int value) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("illegal index"); ensureCapacity(size + 1); for (int i = size; i > index; i--) elementData[i] = elementData[i - 1]; elementData[index] = value; size++; } // pre : 0 <= index < size() // post: removes value at the given index, shifting subsequent values left public void remove(int index) { checkIndex(index); for (int i = index; i < size - 1; i++) elementData[i] = elementData[i + 1]; size--; } // pre : 0 <= index < size() // post: replaces the integer at the given index with the given value public void set(int index, int value) { checkIndex(index); elementData[index] = value; } // post: list is empty public void clear() { size = 0; } // post: ensures that the underlying array has the given capacity; if not, // the size is doubled (or more if given capacity is even larger) public void ensureCapacity(int capacity) { if (capacity > elementData.length) { int newCapacity = elementData.length * 2 + 1; if (capacity > newCapacity) newCapacity = capacity; int[] newList = new int[newCapacity]; for (int i = 0; i < size; i++) newList[i] = elementData[i]; elementData = newList; } } }