// Hunter Schafer, CSE 143 // An ArrayIntList is an ordered list of integers. // ArrayIntLists provide access to elements at a // specific index (0-based). public class ArrayIntList { private int[] elementData; private int size; // post: Constructs an empty ArrayIntList public ArrayIntList() { elementData = new int[10]; size = 0; } // post: Returns the number of elements in this list public int size() { return size; } // post: Adds the given value to the end of the list public void add(int value) { elementData[size] = value; size++; } // pre: index is at least 0 and no larger than size // post: adds the given value to the list at the specific index // shifting all elements to the right to put it there public void add(int index, int value) { // In-line comments like this are for programmers reading our code (not for clients), // feel free to mention implementation details here! // Strategy: scoot over the numbers, add at index for (int i = size - 1; i >= index; i--) { elementData[i + 1] = elementData[i]; } elementData[index] = value; size++; } // post: returns a String representation of this list // surrounded by square brackets and separated by commas // Example: [14, 5, 6, 1] public String toString() { // Does this actually work? Can you write a test case that breaks this? String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } }