CSE142 Program Example handout #30 Client Code IntListTest.java ---------------------------- // Stuart Reges // 12/6/05 // // This program does some basic testing of the IntList class. import java.util.*; public class IntListTest { public static void main(String[] args) { IntList list = new IntList(10); Random r = new Random(); // build up a random list System.out.print("adding:"); for (int i = 0; i < 10; i++) { int next = r.nextInt(100); System.out.print(" " + next); list.add(next); } System.out.println(); // print list two ways System.out.println(list); System.out.print("get returns:"); for (int i = 0; i < list.size(); i++) { System.out.print(" " + list.get(i)); } System.out.println(); // try some sets list.set(0, 1); list.set(3, 2); list.set(9, 3); System.out.println("after setting 0, 3, 9 to 1, 2, 3: " + list); // test remove, emptying the list for (int i = 0; i < 10; i++) { int n = r.nextInt(list.size()); list.remove(n); System.out.println("after removing at " + n + ", list = " + list); } } } Sample Output of IntListTest ---------------------------- adding: 37 64 64 42 74 71 69 82 81 40 [37, 64, 64, 42, 74, 71, 69, 82, 81, 40] get returns: 37 64 64 42 74 71 69 82 81 40 after setting 0, 3, 9 to 1, 2, 3: [1, 64, 64, 2, 74, 71, 69, 82, 81, 3] after removing at 9, list = [1, 64, 64, 2, 74, 71, 69, 82, 81] after removing at 0, list = [64, 64, 2, 74, 71, 69, 82, 81] after removing at 7, list = [64, 64, 2, 74, 71, 69, 82] after removing at 5, list = [64, 64, 2, 74, 71, 82] after removing at 4, list = [64, 64, 2, 74, 82] after removing at 3, list = [64, 64, 2, 82] after removing at 1, list = [64, 2, 82] after removing at 0, list = [2, 82] after removing at 0, list = [82] after removing at 0, list = [] Program File IntList.java ------------------------- // Stuart Reges // 3/9/05 // // 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(int capacity) // constructs an integer list with given capacity // // public int size() // returns the current number of elements in 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 class IntList { private int[] list; // array of integers private int size; // current length of list public static final int DEFAULT_CAPACITY = 100; // pre : capacity >= 0 // post: constructs an empty integer list with the given initial capacity public IntList(int capacity) { this.list = 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.list[index]; } // post: creates a comma-separated, bracketed version of the list public String toString() { String result = "["; if (this.size > 0) { result += this.list[0]; for (int i = 1; i < this.size; i++) { result += ", " + this.list[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.list[i] == value) { return i; } } return -1; } // post: appends the given number to the end of the list public void add(int number) { this.list[this.size] = number; this.size++; } // pre : 0 <= index <= size() // post: inserts the given number at the given index, shifting subsequent // values right public void add(int index, int number) { for (int i = this.size; i > index; i--) { this.list[i] = this.list[i - 1]; } this.list[index] = number; this.size++; } // pre : 0 <= index < size() // post: removes the integer at the given index public void remove(int index) { for (int i = index; i < this.size - 1; i++) { this.list[i] = this.list[i + 1]; } this.size--; } // pre : 0 <= index < size() // post: replaces the integer at the given index with the given value public void set(int index, int value) { this.list[index] = value; } // post: list is empty public void clear() { this.size = 0; } }
Stuart Reges
Last modified: Wed Dec 7 19:40:10 PST 2005