// Allison Obourn // CSE 143 - lecture 2 // An ArrayIntList stores an ordered list of integers. public class ArrayIntList { private int[] elementData; private int size; // 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]; } elementData[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; } // returns the number of elements in the list public int size() { return size; } // returns true if the list is empty and 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(size == 0) { return "[]"; } else { String output = "[" + elementData[0]; for(int i = 1; i < size; i++) { output += ", " + elementData[i]; } return output + "]"; } } }