import java.util.Arrays; /** * Hunter Schafer, CSE 143 * An ArrayIntList is an ordered list of integers * ArrayIntLists provide access to elements at a * specific index (0-based). */ public class ArrayIntList { private int[] elementData; private int size; public static final int DEFAULT_CAPACITY = 10; /** * post: Constructs an empty ArrayIntList with initial capacity * DEFAULT_CAPACITY */ public ArrayIntList() { this(DEFAULT_CAPACITY); } /** * post: constructs a ArrayIntList with the initial capacity given */ public ArrayIntList(int initialCapacity) { elementData = new int[initialCapacity]; size = 0; } /** * post: Returns the number of elements in this list */ public int size() { return size; } /** * Returns the index of the first instance of the given value * Returns -1 if the value is not in the list. */ public int indexOf(int value) { for (int i = 0; i < size; i++) { if (value == elementData[i]) { return i; } } return -1; } /** * post: Adds the given value to the end of the list */ public void add(int value) { add(size, value); } /** * pre: 0 <= index <= size, throws IndexOutOfBoundsException otherwise * post: adds the given value to the list at the specific index * shifting all elements to the right to put it there */ public void add(int index, int value) { checkIndex(index, size + 1); ensureCapacity(size + 1); // In-line comments like this are for programmers reading our code (not for clients), // feel free to mention implementation details here! // Strategy: scoot over the numbers, add at index for (int i = size - 1; i >= index; i--) { elementData[i + 1] = elementData[i]; } elementData[index] = value; size++; } // pre: 0 <= index < size, throws IndexOutOfBoundException if not met public int get(int index) { checkIndex(index, size); return elementData[index]; } /** * pre: 0 <= index < size, throws IndexOutOfBoundException if not met * post: removes the value at the given index, shifting the following values to the left */ public void remove(int index) { // Strategy: shift everything after index to the left checkIndex(index, size); for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; } /** * Throws IndexOutOfBoundsExceptions if the given index is not * between 0 (inclusive) and maxAllowed (exclusive) */ private void checkIndex(int index, int maxAllowed) { if (index < 0 || index >= maxAllowed) { throw new IndexOutOfBoundsException(); } } /** * post: returns a String representation of this list * surrounded by square brackets and separated by commas * Example: [14, 5, 6, 1] */ public String toString() { // Does this actually work? Can you write a test case that breaks this? if (size == 0) { return "[]"; } String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } /** * Increases the capacity if needed to ensure that it can hold at * least the number of elements specified. * * post: elementData.length >= capacity */ private void ensureCapacity(int capacity) { if (elementData.length < capacity) { // double in size until large enough int newCapacity = elementData.length; while (capacity > newCapacity) { newCapacity = newCapacity * 2; } elementData = Arrays.copyOf(elementData, newCapacity); } } }