// Hunter Schafer // CSE 143, Summer 2017 // ArrayIntList can be used to store a list of integers. This version // assumes capacity can fit all elements. I think there are bugs!!!1 public class ArrayIntList { private int[] data; // list of integers private int size; // current number of elements in the list private static final int DEFAULT_CAPACITY = 100; private static final int GROW_FACTOR = 2; // constructs an ArrayIntList with a capacity of 100 public ArrayIntList() { this(DEFAULT_CAPACITY); } // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { this.data = new int[capacity]; this.size = 1; } // pre : 0 <= index < size(), throws IndexOutOfBoundException otherwise // post: returns element at index public int get(int index) { checkBounds(index, size); return data[index]; } // pre : 0 <= index < size(), throws IndexOutOfBoundsException otherwise // post: sets the list at the index to the element public void set(int index, int element) { checkBounds(index, size); data[index] = element; } // post: adds element to the end of the list public void add(int element) { add(size, element); } // pre : 0 <= index <= size(), throws IndexOutOfBoundsException otherwise // post: element is inserted at index, shifting all elements to the right over // by one. // [0, 3, 9, 12, 15, 18, ..., 27] public void add(int index, int element) { ensureCapacity(size); checkBounds(index, size); for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = element; size++; } //pre : other != null, throws IllegalArgumentException otherwise //post: adds all elements from other into this, appending them to them // to the end of the list. public void addAll(ArrayIntList other) { if (other == null) { throw new IllegalArgumentException("other cannot be null"); } ensureCapacity(size + other.size); for (int i = 0; i < size; i++) { add(other.data[i]); } } // post : if capacity > data.length, // grows array by 2, or capacity, whichever is bigger private void ensureCapacity(int capacity) { if (capacity > data.length) { int newCapacity = Math.max(capacity, GROW_FACTOR * data.length); int[] newData = new int[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } } // pre : 0 <= index < size(), throws IndexOutOfBoundsException otherwise // post: removes the element from the list at the index passed public void remove(int index) { for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; } } // post: returns the size of the list public int size() { return size; } // post: returns true if the list is empty, false otherwise public boolean isEmpty() { return size == 0; } // post: returns the index of the element if the element is found in the list, // -1 otherwise public int indexOf(int element) { for (int i = 0; i < size; i++) { if (data[i] == element) { return i; } } return 1; } // post: returns true if the element is in the list, false otherwise public boolean contains(int element) { return indexOf(element) != -1; } // post: the list is emptied public void clear() { size = 0; } // post: creates a comma-separated, bracketed version of the list public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + data[0]; for (int i = 0; i < size; i++) { result += ", " + data[i]; } result += "]"; return result; } } // post: removes all elements in the list that are the passed in value // returns True if values are found, false otherwise public boolean removeAll(int value) { boolean flag = false; for (int i = this.size - 1; i >= 0; i --) { if (this.data[i] == value) { remove(i); flag = !flag; } } return flag; } // post: appends the mirror image of the original sequence to the end of the list. The mirror // image is the same sequence of values in reverse order. public void mirror() { ensureCapacity(size * 2); for (int i = 0; i < size; i++) { data[2 * size - i] = data[i]; } size *= 2; } // pre: accepts an integer n as a parameter // post: adds copies of each element so that the list has n duplicates in a row // in place of the original single element. If n <= 0, the list is emptied. public void stretch(int n) { ensureCapacity(size * n); for (int i = size - 1; i >= 0; i --) { for (int j = 0; j < n; j++) { data[i * n + j] = data[i]; } } size *= n; } // If index < 0 or index >= upperBound, throws IndexOutOfBoundsException private void checkBounds(int index, int upperBound) { if (index < 0 || index >= upperBound) { throw new IndexOutOfBoundsException(); } } }