// Stuart Reges handout #29 // 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() // constructs an integer list of default capacity // 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 void reverse() // reverses the order of the values in the list public class IntList { private int[] list; // array of integers private int size; // 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"); 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) { checkIndex(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) { checkCapacity(); this.list[this.size] = number; this.size++; } // post: inserts the given number at the given index, shifting subsequent // values right public void add(int index, int number) { if (index < 0 || index > this.size) throw new IndexOutOfBoundsException("illegal index"); checkCapacity(); 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) { checkIndex(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) { checkIndex(index); this.list[index] = value; } // post: list is empty public void clear() { this.size = 0; } // post: reverses the integer list public void reverse() { for (int i = 0; i < this.size/2; i++) { int temp = this.list[i]; this.list[i] = this.list[this.size - i - 1]; this.list[this.size - i - 1] = temp; } } // post: throws an exception if the given index is out of range private void checkIndex(int index) { if (index < 0 || index >= this.size) 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 (this.size == this.list.length) { int[] newList = new int[this.list.length * 2 + 1]; for (int i = 0; i < this.size; i++) newList[i] = this.list[i]; this.list = newList; } } }
Stuart Reges
Last modified: Fri Mar 11 10:42:09 PST 2005