Java Reference
In-Depth Information
public boolean contains(T anEntry);
public T remove( int givenPosition);
public void clear();
public int getLength();
public boolean isEmpty();
public T[] toArray();
} // end SortedListInterface
Note: The ADT sorted list can add, remove, or locate an entry, given the entry as an
argument. The sorted list has several operations that are the same as ADT list opera-
tions, namely getEntry , contains , remove (by position), clear , getLength , isEmpty , and
toArray . However, a sorted list will not let you add or replace an entry by position.
Using the ADT Sorted List
16.5
Example. To demonstrate the operations of the ADT sorted list that the previous section specifies,
we first create a sorted list of strings. We begin by declaring and allocating the list nameList , where
we assume that SortedList is an implementation of the ADT operations specified by the interface
SortedListInterface :
SortedListInterface<String> nameList = new SortedList<String>();
Next, we add names in an arbitrary order, realizing that the ADT will organize them alphabetically:
nameList.add("Jamie");
nameList.add("Brenda");
nameList.add("Sarah");
nameList.add("Tom");
nameList.add("Carlos");
The sorted list now contains the following entries:
Brenda
Carlos
Jamie
Sarah
Tom
16.6
Assuming the list just given, here are some examples of the ADT operations on the sorted list:
nameList.getPosition("Jamie") returns 3, the position of Jamie in the list
nameList.contains("Jill") returns false, because Jill is not in the list
nameList.getPosition("Jill") returns - 4, because Jill belongs at position 4 in the list
nameList.getEntry(2) returns Carlos , because he is at position 2 in the list
Now remove To m and the first name in the list by writing
nameList.remove("Tom");
nameList.remove(1);
 
Search WWH ::




Custom Search