// CSE 143, Winter 2012 // An ArrayIntList object stores an ordered list of integers using // an unfilled array. import java.util.Arrays; public class ArrayIntList { public static final int DEFAULT_CAPACITY = 3; private int[] elementData; private int size; // Constructs an empty list of a default capacity. public ArrayIntList() { this(DEFAULT_CAPACITY); } // Constructs an empty list with the given capacity. // Precondition: capacity >= 0 // Throws an illegal argument exception if capacity is negative. public ArrayIntList(int capacity) { if (capacity <= 0) { throw new IllegalArgumentException("capacity must be positive: " + capacity); } elementData = new int[capacity]; size = 0; } // Appends the given value to the end of the list. // Resizes the list's internal array to fit the value if necessary. public void add(int value) { add(size, value); } // Inserts the given value at the given index, shifting subsequent values right. // Precondition: 0 <= index <= size() // Throws an out-of-bounds exception if index is out of range. public void add(int index, int 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 at the given index, shifting subsequent values left. // Precondition: 0 <= index < size() // Throws an out-of-bounds exception if index is out of range. 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] = 0; size--; } // Returns the integer at the given index in the list. // Precondition: 0 <= index < size() // Throws an out-of-bounds exception if index is out of range. public int get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // Sets the given index to store the given value. // Precondition: 0 <= index < size public void set(int index, int value) { checkIndex(index, 0, size - 1); elementData[index] = value; } // Returns the number of elements in the list. public int size() { return size; } // Returns true if the list does not contain any elements. public boolean isEmpty() { return size == 0; } // Returns the first index an element is found, or -1 if not. public int indexOf(int value) { for (int i = 0; i < size ; i++) { if (get(i) == value) { return i; } } return -1; } // Returns true if the list contains the given int value, else false. public boolean contains(int value) { return (indexOf(value) != -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; } // Throws an array index out-of-bounds (OOB) exception if index is not // between min and max inclusive. private void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new ArrayIndexOutOfBoundsException("Invalid index " + index); } } }