// CSE 143, Autumn 2013 // Class ArrayIntList can be used to store a list of integers. It // has no fixed capacity. It dynamically increases in size when necessary. import java.util.*; public class ArrayIntList { public static final int DEFAULT_SIZE = 10; private int[] elementData; // list of integers private int size; // current number of elements in the list // post: constructs an empty list of default capacity public ArrayIntList() { this(DEFAULT_SIZE); } // pre : capacity >= 0 (throws IllegalArgumentException if not) // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } size = 0; elementData = new int[capacity]; } // post: the list will be one longer and the value will // be added to the end public void add(int value) { add(size, value); } // pre : 0 <= index <= size // post: the list will be one longer and the value will be // at index in the list public void add(int index, int value) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("index: " + index); } size++; checkSize(); for(int i = size - 1; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; } // pre : 0 <= index < size if this is not satisfied throws a // IndexOutOfBoundsException // post: returns the value at the passed in index public int get(int index) { checkIndex(index); return elementData[index]; } // pre : 0 <= index < size (throws IndexOutOfBoundsException if not) // post: places the value at the given index in the list public void set(int index, int value) { checkIndex(size); elementData[index] = value; } // post: returns the current number of elements in the list public int size() { return size; } // post: returns true if list is empty, false otherwise public boolean isEmpty() { return size == 0; } // 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: returns 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: elementData is large enough to have at least one elment added // if elementData is too small it doubles in size private void checkSize() { if(size > elementData.length) { int newCapacity = elementData.length * 2 + 1; elementData = Arrays.copyOf(elementData, newCapacity); } } // post: throws an IndexOutOfBoundsException if the given index is // not a legal index of the current list private void checkIndex(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("index: " + index); } } }