// Adam Blank
/** The ArrayIntList class stores an ordered list of integers. */
import java.util.Arrays;
public class ArrayIntList {
private int size;
private int[] data;
public static final int DEFAULT_CAPACITY = 10;
/** Initializes a new array list with initial capacity of initialCapacity integers.
* pre: initialCapacity >= 0 otherwise throws an IllegalArgumentException */
public ArrayIntList(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException();
}
this.data = new int[initialCapacity];
this.size = 0;
}
/** Initializes a new empty list with initial capacity of DEFAULT_CAPACITY integers. */
public ArrayIntList() {
this(DEFAULT_CAPACITY);
}
/** Adds value to the end of the list.
* post: value is appended to the ArrayList */
public void add(int value) {
this.add(size, value);
}
/** Inserts value into the list at index.
* pre: 0 <= index <= size otherwise throws IndexOutOfBoundsException
* post: value is inserted at index; all later values are shifted right by 1 element */
public void add(int index, int value) {
// Increment the size early; so that we can access the element
// at this.size
this.size++;
// Ensure the array as capacity for our new size
this.grow(this.size);
checkIndex(index);
for (int i = this.size - 1; i > index; i--) {
this.set(i, this.get(i-1));
}
this.set(index, value);
}
/** Returns the value at index.
* pre: 0 <= index <= size otherwise throws IndexOutOfBoundsException */
public int get(int index) {
checkIndex(index);
return this.data[index];
}
/** Stores value in index.
* pre: 0 <= index <= size otherwise throws IndexOutOfBoundsException */
public void set(int index, int value) {
checkIndex(index);
this.data[index] = value;
}
/** Removes the value at the given index, shifting later values over.
* pre: 0 <= index < size otherwise throws IndexOutOfBoundsException */
public void remove(int index) {
checkIndex(index);
for (int i = index; i < size - 1; i++) {
this.set(i, this.get(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 a String representation of the list consisting of the elements
* in order, separated by commas and enclosed in square brackets. */
public String toString() {
String result = "";
if (this.size > 0) {
result = "" + this.data[0];
for (int i = 1; i < this.size; i++) {
result += ", " + this.data[i];
}
}
return "[" + result + "]";
}
/** Grows the ArrayIntList to have room for *at least* requiredCapacity elements.
* pre: 0 <= requiredCapacity otherwise throws IllegalArgumentException */
private void grow(int requiredCapacity) {
if (requiredCapacity < 0) {
throw new IllegalArgumentException();
}
if (requiredCapacity > this.data.length) {
this.data = Arrays.copyOf(this.data, this.data.length * 2);
}
}
/** Checks if index is a valid index into this ArrayIntList.
* post: throws IndexOutOfBoundsException if index < 0 or index >= max*/
private void checkIndex(int index) {
if (0 > index || index >= this.size) {
throw new IndexOutOfBoundsException();
}
}
}