// Helene Martin, CSE 143 // An ArrayIntList stores an ordered list of integers. public class ArrayIntList { private int size; private int[] elementData; // Initializes a new empty list with initial capacity of 10 integers. public ArrayIntList() { elementData = new int[10]; // size = 0; // optional; size is initialized to 0 by default } // Adds the given value to the end of the list. // For now we will assume that the array has room to fit the new element. public void add(int value) { elementData[size] = value; size++; } // Inserts the given value into the list at the given index. // For now we will assume that 0 <= index <= size. // For now we will assume that the array has room to fit the new element. public void add(int index, int value) { for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } set(index, value); size++; } // Returns the value at the given index. // For now we will assume that 0 <= index < size. public int get(int index) { return elementData[index]; } // Sets the given index to store the given value. // For now we will assume that 0 <= index < size. public void set(int index, int value) { elementData[index] = value; } // Removes the value at the given index, shifting following values over. public void remove(int index) { for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; } // Returns the number of elements in the list. public int size() { return size; } // Returns true if list is empty, false otherwise. public boolean isEmpty() { return size == 0; } // Returns a String representation of the list consisting of the elements // in order, separated by commas and enclosed in square brackets. public String toString() { if (isEmpty()) { return "[]"; } else { String result = "["; for (int i = 0; i < size - 1; i++) { result += elementData[i] + ", "; } result += elementData[size - 1] + "]"; return result; } } }