// Adam Blank
/** The ArrayIntList class stores an ordered list of integers. */
public class ArrayIntList {
private int size;
private int[] data;
/** Initializes a new empty list with initial capacity of 10 integers. */
public ArrayIntList() {
this.data = new int[10];
this.size = 0;
}
/** Adds value to the end of the list.
* CAUTION: This method does not work properly if the array doesn't
* have room for the new element. We will fix this later. */
public void add(int value) {
this.set(size, value);
this.size++;
}
/** Inserts value into the list at index.
* CAUTION: This method does not work properly if the array doesn't
* have room for the new element. We will fix this later.
* CAUTION: This method does not work properly if index < 0 or
* index > size. We will fix this later. */
public void add(int index, int value) {
for (int i = size; i > index; i--) {
this.set(i, this.get(i-1));
}
this.set(index, value);
this.size++;
}
/** Returns the value at index.
* CAUTION: This method does not work properly if index < 0 or
* index > size. We will fix this later. */
public int get(int index) {
return this.data[index];
}
/** Stores value in index.
* CAUTION: This method does not work properly if index < 0 or
* index > size. We will fix this later. */
public void set(int index, int value) {
this.data[index] = value;
}
/** Removes the value at the given index, shifting later values over.
* CAUTION: This method does not work properly if index < 0 or
* index > size. We will fix this later.
* CAUTION: This method does not work correctly if size == 0 */
public void remove(int 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 + "]";
}
}