// CSE 143, Winter 2009, Marty Stepp // An ArrayAnythingList object stores a list of values using // an unfilled array as its underlying data structure. // This version uses "generics" so that it can store any type of value, not // just integers. It also implements the same interface as LinkedAnythingList, // so that the two can be used interchangeably in client code. public class ArrayAnythingList extends AbstractAnythingList { public static final int DEFAULT_CAPACITY = 100; // data (fields) private E[] elementData; // stores the list's elements private int size; // number of elements in the list // Initializes a new empty list that can hold up to 1000 integers. public ArrayAnythingList() { this(DEFAULT_CAPACITY); } // Initializes a new empty list that can hold up to the given number of integers. // We say "suppress warnings" on the next line to avoid a compiler warning // about casting from Object[] to E[]. @SuppressWarnings("unchecked") public ArrayAnythingList(int capacity) { elementData = (E[]) (new Object[capacity]); size = 0; } // Inserts the given value into the list at the given index, // sliding subsequent elements right to make room for it. // Precondition: 0 <= index <= size public void add(int index, E value) { checkIndex(index, 0, size); ensureCapacity(); // shift elements right to make room for the new element for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // Returns the value in the list at the given index. // Precondition: 0 <= index < size public E get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // Returns the first index at which the given value is found in this list. // If the value is not found, returns -1. public int indexOf(E value) { for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; // not found } // Removes the element from the given index from this list, // sliding subsequent elements left to fill the gap created. // Precondition: 0 <= index < size public void remove(int index) { checkIndex(index, 0, size - 1); for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; } // Sets the given index to store the given value. // Precondition: 0 <= index < size public void set(int index, E value) { checkIndex(index, 0, size - 1); elementData[index] = value; } // Returns the number of elements in the list. public int size() { return size; } // Checks that the given index is between the given min/max inclusive. // If not, throws an ArrayIndexOutOfBoundsException. private void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new ArrayIndexOutOfBoundsException("Index out of bounds: " + index); } } // Makes sure that the array has room to fit another element. // If not, doubles the size of the array. @SuppressWarnings("unchecked") private void ensureCapacity() { if (size >= elementData.length) { // out of space; double array length E[] biggerData = (E[]) (new Object[2 * elementData.length]); for (int i = 0; i < size; i++) { biggerData[i] = elementData[i]; } elementData = biggerData; } } }