// Textbook fragment 06.03 /** Realization of an indexed list by means of an array, which is doubled * when the size of the indexed list exceeds the capacity of the array. */ public class ArrayIndexList implements IndexList { private E[] A; // array storing the elements of the indexed list private int capacity = 16; // initial length of array A private int size = 0; // number of elements stored in the indexed list /** Creates the indexed list with initial capacity 16. */ public ArrayIndexList() { A = (E[]) new Object[capacity]; // the compiler may warn, but this is ok } /** Inserts an element at the given index. */ public void add(int r, E e) throws IndexOutOfBoundsException { checkIndex(r, size() + 1); if (size == capacity) { // an overflow capacity *= 2; E[] B =(E[]) new Object[capacity]; for (int i=0; i=r; i--) // shift elements up A[i+1] = A[i]; A[r] = e; size++; } /** Removes the element stored at the given index. */ public E remove(int r) throws IndexOutOfBoundsException { checkIndex(r, size()); E temp = A[r]; for (int i=r; i