Java Reference
In-Depth Information
int numberOfEntries = list.getLength();
System.out.println("The list contains " + numberOfEntries +
" entries, as follows:");
for ( int position = 1; position <= numberOfEntries; position++)
System.out.println(list.getEntry(position) +
" is entry " + position);
System.out.println();
} // end displayList
} // end ListClient
Output
The list contains 4 entries, as follows:
16 is entry 1
4 is entry 2
33 is entry 3
27 is entry 4
The data type of displayList 's input parameter list is ListInterface<String> . Thus, the
argument of the method must be an object that satisfies both of the following conditions:
The object's class must implement ListInterface .
The object must be instantiated as a list of strings.
Although the method works for any implementation of the ADT list, it works only for lists of
strings. You could remove the latter restriction by revising the header of the method as follows:
public static <T> void displayList(ListInterface<T> list)
Now the list passed to the method can contain objects of any one class.
Note: A reminder
Notice that the data type of runnerList is ListInterface<String> . This declaration obliges
runnerList to call only methods in the interface and to add only strings to the list. If the data
type was AList<String> instead, runnerList would be able to call any public methods in
AList even if they were not declared in ListInterface .
Question 3 In the previous example, what changes to testList are necessary to represent
the runner's numbers as Integer objects instead of strings? Use Java's auto-boxing feature,
as described in Segment A.99 of Appendix A.
12.9
Example. A professor wants an alphabetical list of the names of the students who arrive for class
today. As each student enters the room, the professor adds the student's name to a list. It is up to the
professor to place each name into its correct position in the list so that the names will be in alpha-
betical order. The ADT list does not choose the order of its entries.
The following Java statements place the names Amy, Ellen, Bob, Drew, Aaron, and Carol in an
alphabetical list. The comment at the end of each statement shows the list after the statement executes.
Search WWH ::




Custom Search