CSE143 Sample Program handout #3 Client Program IntListSample.java --------------------------------- // Demonstrates some basic use of IntList public class IntListSample { public static void main(String[] args) { IntList list = new IntList(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); for (int i = 1; i < 10; i += 2) System.out.println("index of " + i + " = " + list.indexOf(i)); for (int i = 0; i < list.size(); i++) System.out.println("get for " + i + " returns " + list.get(i)); 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 IntListSample.java ---------------------------- initial list = [3, 7, 11] after some adds = [2, 3, 5, 7, 11] index of 1 = -1 index of 3 = 1 index of 5 = 2 index of 7 = 3 index of 9 = -1 get for 0 returns 2 get for 1 returns 3 get for 2 returns 5 get for 3 returns 7 get for 4 returns 11 list = [2, 3, 5, 7, 11] after removing at 1 list = [2, 5, 7, 11] after removing at 2 list = [2, 5, 11] after removing at 1 list = [2, 11] after removing at 1 list = [2] after removing at 0 list = [] Program IntList.java -------------------- // Class IntList can be used to store a list of integers. public class IntList { 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 IntList() { this(DEFAULT_CAPACITY); } // pre : capacity >= 0 // post: constructs an empty list with the given capacity public IntList(int capacity) { this.elementData = new int[capacity]; this.size = 0; } // post: returns the current number of elements in the list public int size() { return this.size; } // pre : 0 <= index < size() // post: returns the integer at the given index in the list public int get(int index) { return this.elementData[index]; } // post: creates a comma-separated, bracketed version of the list public String toString() { String result = "["; if (this.size > 0) { result += this.elementData[0]; for (int i = 1; i < this.size; i++) result += ", " + this.elementData[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 < this.size; i++) if (this.elementData[i] == value) return i; return -1; } // pre : size() < capacity (elementData.length) // post: appends the given value to the end of the list public void add(int value) { this.elementData[this.size] = value; this.size++; } // pre: size() < capacity (elementData.length) && 0 <= index <= size() // post: inserts the given value at the given index, shifting subsequent // values right public void add(int index, int value) { for (int i = this.size; i > index; i--) this.elementData[i] = this.elementData[i - 1]; this.elementData[index] = value; this.size++; } // pre : 0 <= index < size() // post: removes value at the given index, shifting subsequent values left public void remove(int index) { for (int i = index; i < this.size - 1; i++) this.elementData[i] = this.elementData[i + 1]; this.size--; } }
Stuart Reges
Last modified: Fri Jan 6 14:04:07 PST 2006