//---------------------------------------------------------------------------- // ArraySortedList3.java by Dale/Joyce/Weems Chapter 6 // // Implements the ListInterface using an array. It is kept in increasing order // as defined by the compareTo method of the added elements. Only Comparable // elements may be added to a list. // // Null elements are not permitted on a list. // // Overrides the unsorted version of find with a recursive binary search // // ajg version: DWC, T extends Comparable, format, while-->for, new add() //---------------------------------------------------------------------------- package ch06.lists; public class ArraySortedList3> extends ArrayUnsortedList implements ListInterface { public ArraySortedList3(T[] list) { super(list); } public void add(T element) { if (numElements == list.length) throw new ListOverflowException("Helpful message."); // slide existing elements down to make a slot for new element for (int location=numElements++; location>0; location--) { if (element.compareTo(list[location-1]) >= 0) break; // new element goes here list[location] = list[location-1]; } list[location] = element; } // Remove an element e such that e.equals(element). Return outcome. public boolean remove (T element) { boolean found = find(element); if (found) { for (int i=location; i <= numElements-2; i++) list[i] = list[i+1]; list[--numElements] = null; } return found; } // Recursive binary search protected boolean find(T target) { return find1 (target, 0, numElements-1); } private boolean find1 (T target, int lo, int hi) { if (lo > hi) return false; location = (lo + hi) / 2; if (list[location].compareTo(target) == 0) // found it return true; if (list[location].compareTo(target) < 0) // too small return find1 (target, location+1, hi); return find1 (target, lo, hi-1); // too big } } // Local Variables: // compile-command: "cd ../..; javac ch06/lists/ArraySortedList3.java" // End: