// AbstractIntList provides a skeletal implementation of the IntList class. // Many operations are defined in terms of the list iterator. Subclasses // need to define size(), get(int index), add(int value), add(index, value), // remove(index), set(index, value), clear(), and iterator(). import java.util.*; public abstract class AbstractIntList implements IntList { // post: returns a comma-separated, bracketed version of the list public String toString() { Iterator itr = iterator(); String result = "["; if (itr.hasNext()) { result += itr.next(); while (itr.hasNext()) { result += ", " + itr.next(); } } result += "]"; return result; } // post : returns the position of the first occurence of the given // value (-1 if not found) public int indexOf(int value) { int index = 0; for (int n : this) { if (n == value) { return index; } index++; } return -1; } // post: returns true if list is empty, false otherwise public boolean isEmpty() { return !iterator().hasNext(); } // post: returns true if the given value is contained in the list, // false otherwise public boolean contains(int value) { return indexOf(value) != -1; } // post: appends all values in the given list to the end of this list public void addAll(IntList other) { for (int n : other) { add(n); } } // post: removes all occurrences of the values in the given list from // this list public void removeAll(IntList other) { Iterator itr = iterator(); while (itr.hasNext()) { if (other.contains(itr.next())) { itr.remove(); } } } // post: throws an exception if the given index is out of range protected void checkIndex(int index) { if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException("index: " + index); } } }