// CSE 143, Winter 2010, Marty Stepp // An ArrayIntList object stores an ordered list of integers using // an unfilled array. // // This is an unfinished "in-progress" version of the class. // We will work on it more in Friday's lecture. // // This version of the class adds the following: // - remove method // - indexOf, contains, and isEmpty methods // - second constructor that accepts a parameter for capacity // - checking indexes and throwing exceptions // // NOTE: Please don't use this file with your HW2 SortedIntList homework. // Use the version of ArrayIntList.java posted on the Homework web page. public class ArrayIntList { private int[] elementData; // stores the list's elements private int size; // number of elements in the list // Initializes a new empty list with initial capacity of 10 integers. public ArrayIntList() { this(10); } // 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. public void add(int index, int value) { checkIndex(index, 0, size); checkResize(); // 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++; } // Inserts the given value into the list at the given index. // If necessary, resizes the array to fit the value. // Precondition: 0 <= index <= size public void remove(int index) { // 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; } // 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 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 value in the list at the given index. // Precondition: 0 <= index < size public int get(int index) { checkIndex(index, 0, size - 1); return elementData[index]; } // A "helper" method that throws an IllegalArgumentException if the given // index is not between the given min and max, inclusive. public void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new IllegalArgumentException("booyah! you SUCK"); } } // Returns the number of elements in the list. public int size() { return size; } // Prints the elements of the list, one per line. public void print() { for (int i = 0; i < size; i++) { System.out.println(elementData[i]); } } // Checks whether the list's array is full, and if so, // doubles its size so that more elements can be added. public void checkResize() { if (size >= elementData.length) { // copy elements to a new larger array int[] newOne = new int[size * 2]; for (int i = 0; i < size; i++) { newOne[i] = elementData[i]; } // replace old array with new array elementData = newOne; } } }