CSE143 Sample Program handout #2
Client Program ArrayIntListSample.java
--------------------------------------
public class ArrayIntListSample {
public static void main(String[] args) {
ArrayIntList list = new ArrayIntList(25);
list.add(3);
list.add(7);
list.add(11);
System.out.println("initial list = " + list);
list.add(0, 2);
list.add(2, 5);
System.out.println("after some adds = " + list);
System.out.print("index of:");
for (int i = 1; i < 10; i += 2)
System.out.print(" " + i + " -> " + list.indexOf(i));
System.out.println();
System.out.print("get:");
for (int i = 0; i < list.size(); i++)
System.out.print(" " + i + " -> " + list.get(i));
System.out.println();
System.out.println("list = " + list);
while (list.size() > 0) {
int i = (int) (Math.random() * list.size());
list.remove(i);
System.out.println("after removing at " + i + " list = " + list);
}
}
}
Output of ArrayIntListSample.java
---------------------------------
initial list = [3, 7, 11]
after some adds = [2, 3, 5, 7, 11]
index of: 1 -> -1 3 -> 1 5 -> 2 7 -> 3 9 -> -1
get: 0 -> 2 1 -> 3 2 -> 5 3 -> 7 4 -> 11
list = [2, 3, 5, 7, 11]
after removing at 4 list = [2, 3, 5, 7]
after removing at 0 list = [3, 5, 7]
after removing at 0 list = [5, 7]
after removing at 0 list = [7]
after removing at 0 list = []
Program ArrayIntList.java
-------------------------
// Class ArrayIntList can be used to store a list of integers.
public class ArrayIntList {
private int[] elementData; // list of integers
private int size; // current number of elements in the list
public static final int DEFAULT_CAPACITY = 100;
// post: constructs an empty list of default capacity
public ArrayIntList() {
this(DEFAULT_CAPACITY);
}
// pre : capacity >= 0 (throws IllegalArgumentException if not)
// post: constructs an empty list with the given capacity
public ArrayIntList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity: " + capacity);
}
elementData = new int[capacity];
size = 0;
}
// post: returns the current number of elements in the list
public int size() {
return size;
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: returns the integer at the given index in the list
public int get(int index) {
checkIndex(index);
return elementData[index];
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
if (size == 0) {
return "[]";
} else {
String result = "[" + elementData[0];
for (int i = 1; i < size; i++) {
result += ", " + elementData[i];
}
result += "]";
return result;
}
}
// post : returns the position of the first occurrence of the given
// value (-1 if not found)
public int indexOf(int value) {
for (int i = 0; i < size; i++) {
if (elementData[i] == value) {
return i;
}
}
return -1;
}
// post: returns true if list is empty, false otherwise
public boolean isEmpty() {
return size == 0;
}
// post: returns true if the given value is contained in the list,
// false otherwise
public boolean contains(int value) {
return indexOf(value) >= 0;
}
// pre : size() < capacity (throws IllegalStateException if not)
// post: appends the given value to the end of the list
public void add(int value) {
add(size, value);
}
// pre : size() < capacity (throws IllegalStateException if not) &&
// 0 <= index <= size() (throws IndexOutOfBoundsException if not)
// post: inserts the given value at the given index, shifting subsequent
// values right
public void add(int index, int value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index);
}
if (size + 1 > elementData.length) {
throw new IllegalStateException("would exceed list capacity");
}
for (int i = size; i > index; i--) {
elementData[i] = elementData[i - 1];
}
elementData[index] = value;
size++;
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: removes value at the given index, shifting subsequent values left
public void remove(int index) {
checkIndex(index);
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
size--;
}
// post: throws an IndexOutOfBoundsException if the given index is
// not a legal index of the current list
private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index: " + index);
}
}
}