// CSE 143, Winter 2012 // An ArrayIntList object stores an ordered list of integers using // an unfilled array. // // Today's version adds: //* toString method so that clients can easily print lists, //* indexOf and contains methods for searching lists, //* second constructor that accepts a capacity parameter, //* checks preconditions on several methods (add, get, set, remove) and //throws exceptions if the indexes passed are out of range. import java.util.Arrays; public class ArrayIntList { public static final int DEFAULT_CAPACITY = 3; private int[] elementData; private int size; // initializes a new empty list with initial capacity of 10 integers. public ArrayIntList() { this(DEFAULT_CAPACITY); } // initializes a new empty list with the given initial capacity. // pre: capacity > 0 public ArrayIntList(int capacity) { if (capacity <= 0) { throw new IllegalArgumentException("capacity must be positive: " + capacity); } elementData = new int[capacity]; size = 0; } // adds the given value to the end of the list. // resize if necessary public void add(int value) { add(size, value); } // inserts the given value into the list at the given index. // resize if necessary // pre: 0 <= index <= size (can add at very end) 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 from the given index, shifting following elements left // by 1 slot to cover the hole. // pre: 0 <= index < size 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 value in the list at the given index. // pre: 0 <= index < size public int get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // sets the given index to store the given value. // pre: 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 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; } // if index is not within bounds, throw exception private void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new ArrayIndexOutOfBoundsException("Invalid index " + index); } } }