// CSE 143, Winter 2010, Marty Stepp // An ArrayIntList object stores an ordered list of integers using // an unfilled array. // // This third and final version of the class adds the following: // - constant for default capacity // - toString method // - protected fields and methods // - comprehensive pre/postconditions, comments, and exception tests // - ensureCapacity method for better resizing // // NOTE: Please don't use this file with your HW2 SortedIntList homework. // Use the version of ArrayIntList.java posted on the Homework web page. import java.util.*; // for Arrays class public class ArrayIntList { // default value used for the capacity of new ArrayIntLists if none is provided public static final int DEFAULT_CAPACITY = 10; // by making these fields 'protected', ArrayIntList and subclasses can see them protected int[] elementData; // stores the list's elements protected int size; // number of elements in the list // Initializes a new empty list with initial capacity of 10 integers. public ArrayIntList() { this(DEFAULT_CAPACITY); } // Initializes a new empty list with the given initial capacity. // Precondition: capacity >= 0 public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } // Adds the given value to the end of the list. // If necessary, resizes the array to fit the value. public void add(int value) { add(size, value); } // Inserts the given value into the list at the given index. // If necessary, resizes the array to fit the value. // Precondition: 0 <= index <= size. // Throws an IndexOutOfBoundsException if index is out of range. public void add(int index, int value) { checkIndex(index, 0, size); // checkResize(); ensureCapacity(size + 1); // make room (shift elements to the right) for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } // add the element elementData[index] = value; size++; } // Returns true if the list contains the given value, otherwise false. public boolean contains(int value) { // if (indexOf(value) != -1) { // return true; // } else { // return false; // } return indexOf(value) != -1; // "Boolean Zen" } // Returns the first index in the list where the given value is found. // If the value is not found in the list, returns -1. public int indexOf(int value) { for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; } // Returns true if the list does not contain any elements, otherwise false. public boolean isEmpty() { // if (size == 0) { // return true; // } else { // return false; // } return size == 0; // "Boolean Zen" } // Returns the value in the list at the given index. // Precondition: 0 <= index < size // Throws an IndexOutOfBoundsException if index is out of range. public int get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // Inserts the given value into the list at the given index. // If necessary, resizes the array to fit the value. // Precondition: 0 <= index <= size // Throws an IndexOutOfBoundsException if index is out of range. public void remove(int index) { checkIndex(index, 0, size - 1); // shift elements to the left to cover up the removed element for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; elementData[size] = 0; } // Sets the given index to store the given value. // Precondition: 0 <= index < size // Throws an IndexOutOfBoundsException if index is out of range. public void set(int index, int value) { checkIndex(index, 0, size - 1); elementData[index] = value; } // Returns the number of elements in the list. public int size() { return size; } // Prints the elements of the list, one per line. // Example: "[4, -1, 19, 45, 28957]" // Example (empty list): "[]" public String toString() { if (isEmpty()) { return "[]"; } else { // cumulative sum --> cumulative string concatenation String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; // "2 1 3 " + 7 --> "2 1 3 7" } result += "]"; return result; } } // A "helper" method that throws an IllegalArgumentException if the given // index is not between the given min and max, inclusive. protected void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new IndexOutOfBoundsException("bad index: " + index); } } // A "helper" method that makes sure that this list's internal array is large // enough to store the given number of elements. // Precondition: capacity >= 0 // Postcondition: elementData.length >= capacity protected void ensureCapacity(int capacity) { // double in size until large enough while (capacity > elementData.length) { elementData = Arrays.copyOf(elementData, 2 * elementData.length); } } }