// Helene Martin, CSE 143 // An ArrayIntList stores an ordered list of integers. // Today's version added: //* indexOf and contains methods for searching lists //* second constructor that accepts a capacity parameter //* precondition comments and checking on several methods (add, get, set, remove) import java.util.Arrays; public class ArrayIntList { private int size; private int[] elementData; public static final int DEFAULT_CAPACITY = 10; // Initializes a new empty list with initial capacity of 10 integers. public ArrayIntList() { // elementData = new int[10]; this(DEFAULT_CAPACITY); } // Initializes a new empty list with specified initial capacity. // pre: capacity > 0, throws IllegalArgumentException otherwise public ArrayIntList(int capacity) { if (capacity <= 0) { throw new IllegalArgumentException("capacity must be >= 0: " + capacity); } elementData = new int[capacity]; // size = 0; // optional; size is initialized to 0 by default } // Adds the given value to the end of the list. public void add(int value) { add(size, value); } // Inserts the given value into the list at the given index. // pre: 0 <= index <= size (can add at very end), throws // ArrayIndexOutOfBoundsException otherwise public void add(int index, int value) { checkIndex(index, 0, size); if (size == elementData.length) { elementData = Arrays.copyOf(elementData, size * 2); } for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // Returns the value at the given index. // pre: 0 <= index < size, throws ArrayIndexOutOfBoundsException otherwise public int get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // Sets the given index to store the given value. // pre: 0 <= index < size, throws ArrayIndexOutOfBoundsException otherwise public void set(int index, int value) { checkIndex(index, 0, size - 1); elementData[index] = value; } // Removes the value at the given index, shifting following values over. // pre: 0 <= index < size, throws ArrayIndexOutOfBoundsException otherwise public void remove(int index) { checkIndex(index, 0, size - 1); 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 the first index of value or -1 if not found. public int indexOf(int value) { for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; } // Returns true if the value is in the list, false otherwise. public boolean contains(int value) { return indexOf(value) != -1; /* * if (indexOf(value) != -1) { return true; } else { return false; } */ } // 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; } } // If index is not within specified bounds bounds, throws // ArrayIndexOutOfBoundsException private void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new ArrayIndexOutOfBoundsException("Invalid index " + index); } } }