// AbstractIntList provides a skeletal implementation of the IntList class.
// Subclasses need to define size(), get(int index), add(int value),
// add(index, value), remove(index), set(index, value), and clear().
// (GY edited from SR)
// This is class is abstract, meaning that it cannot be instantiated.
// Only a descendent, filling in all the missing holes, can be instantiated.
public abstract class SimpleAbstractIntList implements SimpleIntList {
// post: returns true if list is empty, false otherwise
// Any implementation (Array/LinkedList) will use the exact same code,
// so we can extract the common code and put it here. As we learn
// iterators, we will be able to put more code here as well.
public boolean isEmpty() {
return size()==0;
// The so-called boolean zen, preferable to doing (size() == 0) == true
}
// post: throws an exception if the given index is out of range
// This is a handy routine to make sure an index parameter is
// within the legal range. It is protected, because the outside world
// shouldn't see it, and so descendents of this class can use it.
protected void checkIndex(int index) {
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException("illegal index, size: "
+ size() + ", index: " + index);
// My personal preference for exceptions is to include all the
// information necessary to debug the exception. This is more
// than a simple error message, but less than a enduser-friendly
// message. The main caveat is not to cause another exception
// while building the message.
}
}