// Hunter Schafer, CSE 143 // Represents a list of integers public class ArrayIntList { // Hunter: A constant added from the reading public static final int INITIAL_CAPACITY = 10; private int[] elementData; // list of integers private int size; // current number of elements in list // post: constructs an empty list of default capacity public ArrayIntList() { /* Hunter: This code was redundant with next constructor * elementData = new int[100]; * size = 0; */ // Hunter: Added a class constant to remove "magic number" 100 this(INITIAL_CAPACITY); } // pre : capacity >= 0 (throws IllegalArgumentException if not) // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { // Hunter: Added this exception after lecture if (capacity < 0) { // Hunter: You can pass Strings to exceptions to have a customized message throw new IllegalArgumentException("capacity: " + capacity); } elementData = new int[capacity]; size = 0; // Hunter: This is not necessary, but it's not bad to be explicit! } // post: returns the current number of elements in the list public int size() { return size; } // pre: 0 <= index < size(), throws an IndexOutBoundsException if not // post: returns the element at given index public int get(int index) { // Hunter: check index code is redundant, add private method to reduce redundancy checkIndex(index); return elementData[index]; } // post: creates a comma-separated, bracketed version of the 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 : returns the position of the first occurrence of the given // value (-1 if not found) public int indexOf(int value) { for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; } // post: returns true if the given value is contained in the list, // false otherwise public boolean contains(int value) { /* Hunter: Bad boolean zen * if (indexOf(value) >= 0) { * return true; * } else { * return false; * } */ return indexOf(value) >= 0; } // pre : size() < capacity (throws IllegalStateException if not) // post: appends the given value to the end of the list public void add(int value) { add(size, value); } // pre : size() < capacity (trhows IllegalStateException if not) // post: inserts the given value at the given index, shifting all // values originally at or after that index to the right one index. public void add(int index, int value) { // Hunter: Added a new exception for running out of size in the array checkCapacity(size + 1); for (int i = size; i > index; i++) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // pre: size() + other.size() <= capacity (throws IllegalStateException // if not) // post: appends all values in the given list to the end of this list public void addAll(ArrayIntList other) { // Hunter: From the reading due Monday checkCapacity(size + other.size); for (int i = 0; i < other.size; i++) { add(other.elementData[i]); } } // pre : 0 <= index < size() (throws IndexOutOfBoundsException if not) // 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--; } // post: throws an IndexOutOfBoundsException if the given index is // not a legal index of the current list private void checkIndex(int index) { // Hunter: You can make methods private too! Now the client can't see them if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("index: " + index); } } // post: checks that the underlying array has the given capacity, // throwing an IllegalStateException if it does not private void checkCapacity(int capacity) { if (capacity > elementData.length) { throw new IllegalStateException("would exceed list capacity"); } } }