// Stuart Reges handout #33
// 12/8/04
//
// Class IntList can be used to store a list of integers. It has several
// methods that involve indexing the list. As with Java arrays and Strings,
// index values start with 0. This variation of the IntList will expand if
// necessary to a larger capacity. Class IntList has the following public
// methods:
//
// public IntList()
// constructs an integer list of default capacity
// public IntList(int capacity)
// constructs an integer list with given capacity
//
// public int size()
// returns the current length of the list
// public int get(int index)
// returns the integer at the given index
// public String toString()
// returns a String representation of the list
// public int indexOf(int value)
// returns the index of the given value in the list, -1 if not found
//
// public void add(int number)
// appends the given number to the end of the list
// public void add(int index, int number)
// inserts the given number at the given index, shifting subsequent values
// right
// public void remove(int index)
// removes the value at the given index, shifting subsequent elements left
// public void set(int index, int number)
// replaces the value at the given index with the given value
// public void clear()
// removes all elements from the list making it empty
// public void reverse()
// reverses the order of the values in the list
public class IntList {
private int[] myList; // array of integers
private int myLength; // current length of list
public static final int DEFAULT_CAPACITY = 100;
// post: constructs an empty integer list of default capacity
public IntList() {
this(DEFAULT_CAPACITY);
}
// pre : capacity >= 0
// post: constructs an empty integer list with the given initial capacity
public IntList(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException("negative capacity");
myList = new int[capacity];
myLength = 0;
}
// post: returns the current length of the list
public int size() {
return myLength;
}
// pre : 0 <= index < size()
// post: returns the integer at the given index in the list
public int get(int index) {
checkIndex(index);
return myList[index];
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
String result = "[";
if (myLength > 0) {
result += myList[0];
for (int i = 1; i < myLength; i++)
result += ", " + myList[i];
}
result += "]";
return result;
}
// post : returns the position of the first occurence of the given
// value (-1 if not found)
public int indexOf(int value) {
for(int i = 0; i < myLength; i++)
if (myList[i] == value)
return i;
return -1;
}
// post: appends the given number to the end of the list
public void add(int number) {
checkCapacity();
myList[myLength] = number;
myLength++;
}
// post: inserts the given number at the given index, shifting subsequent
// values right
public void add(int index, int number) {
if (index < 0 || index > myLength)
throw new IndexOutOfBoundsException("illegal index");
checkCapacity();
for (int i = myLength; i > index; i--)
myList[i] = myList[i - 1];
myList[index] = number;
myLength++;
}
// pre : 0 <= index < size()
// post: removes the integer at the given index
public void remove(int index) {
checkIndex(index);
for (int i = index; i < myLength - 1; i++)
myList[i] = myList[i + 1];
myLength--;
}
// pre : 0 <= index < size()
// post: replaces the integer at the given index with the given value
public void set(int index, int value) {
checkIndex(index);
myList[index] = value;
}
// post: list is empty
public void clear() {
myLength = 0;
}
// post: reverses the integer list
public void reverse() {
for (int i = 0; i < myLength/2; i++) {
int temp = myList[i];
myList[i] = myList[myLength - i - 1];
myList[myLength - i - 1] = temp;
}
}
// post: throws an exception if the given index is out of range
private void checkIndex(int index) {
if (index < 0 || index >= myLength)
throw new IndexOutOfBoundsException("illegal index");
}
// post: ensures that the array has room for a new value; if not, the
// capacity is doubled
private void checkCapacity() {
if (myLength == myList.length) {
int[] newList = new int[myList.length * 2 + 1];
for (int i = 0; i < myLength; i++)
newList[i] = myList[i];
myList = newList;
}
}
}