/* * Kyle Pierce * CSE 143 * * Class ArrayIntList is used to store an ordered sequence of integers. * This version is relatively complete and has good style. */ public class ArrayIntList { private int[] elementData; // all the numbers in the list private int size; // how many numbers a client thinks there are public static final int DEFAULT_CAPACITY = 100; // post: initializes an ArrayIntList of default capacity public ArrayIntList() { this(DEFAULT_CAPACITY); } // post: initializes an ArrayIntList to given initialCapacity public ArrayIntList(int initialCapacity) { elementData = new int[initialCapacity]; size = 0; } // post: returns the current size of this list public int size() { return size; } // pre: 0 <= index < size (throws IndexOutOfBoundsException if not met) // post: returns the value at the given index public int get(int index) { checkIndex(index); return elementData[index]; } // pre: 0 <= index < size (throws IndexOutOfBoundsException if not met) // post: sets the number at the given index to the given value public void set(int index, int value) { checkIndex(index); elementData[index] = value; } // post: returns true if the given value is in the list and false otherwise public boolean contains(int value) { return (indexOf(value) >= 0); } // post: returns the first index of the given value in the given list // or -1 if the value is not found public int indexOf(int value) { for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; } // pre: list has room for another value (IllegateStateException if not) // post: adds the given value at the end of the list public void add(int value) { add(size, value); } // pre: list has room for another value (IllegateStateException if not) // 0 <= index <= size (throws IndexOutOfBoundsException if not met) // post: adds the given value at the given index in the list, shifting // subsequent values to the right public void add(int index, int value) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("index: " + index); } checkCapacity(size + 1); for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // pre: this.size() + other.size() does not exceed this list's capacity // post: appends all values from the other list to this list without // modifying the given other list public void addAll(ArrayIntList other) { checkCapacity(size + other.size); for (int i = 0; i < other.size; i++) { add(other.elementData[i]); } } // pre: 0 <= index < size (throws IndexOutOfBoundsException if not met) // post: removes the value at the given index from the list, shifting // subsequent values to the left public void remove(int index) { checkIndex(index); for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; } // post: completely empties the list public void clear() { size = 0; } // post: returns a bracketed, comma-separated String of all // the numbers in this list public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } } // post: throws IllegalStateException if capacity exceeds the length // of the elementData field private void checkCapacity(int capacity) { if (capacity > elementData.length) { throw new IllegalStateException("would exceed list capacity"); } } // post: throws IndexOutOfBoundsException if index < 0 or index >= size private void checkIndex(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("index: " + index); } } }