import java.util.Iterator; // Helene Martin, CSE 143 // This class provides a skeletal implementation of the List interface // to minimize the effort required to implement it. // Abstract classes are a good way to provide a partially implemented class. // This class compiles despite not implementing all of the List interface's methods // because abstract classes can't be directly instantiated and must instead be extended. // A subclass of AbstractList that doesn't implement the rest of the List interface will // not compile. public abstract class AbstractList implements List { // Adds the given value to the end of the list. public void add(E value) { add(size(), value); } // Returns true if the value is in the list, false otherwise. public boolean contains(E value) { return indexOf(value) != -1; } // Returns true if list is empty, false otherwise. public boolean isEmpty() { return size() == 0; } // Adds all the elements from other to this list. public void addAll(List other) { // Note: without iterators, this is O(n^2) for LinkedLists // for (int i = 0; i < other.size(); i++) { // add(other.get(i)); // } Iterator itr = other.iterator(); while (itr.hasNext()) { E value = itr.next(); add(value); } } }