Java Reference
In-Depth Information
the list is empty, givenPosition < 1, or
givenPosition > getLength() */
public T getEntry( int givenPosition);
/** Sees whether this list contains a given entry.
@param anEntry the object that is the desired entry
@return true if the list contains anEntry, or false if not */
public boolean contains(T anEntry);
/** Gets the length of this list.
@return the integer number of entries currently in the list */
public int getLength();
/** Sees whether this list is empty.
@return true if the list is empty, or false if not */
public boolean isEmpty();
/** Retrieves all entries that are in this list in the order in which
they occur in the list. */
public T[] toArray();
} // end ListInterface
Question 1 Write pseudocode statements that add some objects to a list, as follows. First
add c , then a , then b , and then d , such that the order of the objects in the list will be a , b , c , d .
Question 2 Write pseudocode statements that exchange the third and seventh entries in a list
of 10 objects.
Note: The entries in a list of n entries are numbered from 1 to n . Although you cannot add
a new entry at position 0, you can add one at position n + 1.
Using the ADT List
Imagine that we hire a programmer to implement the ADT list in Java, given the interface and
specifications that we have developed so far. If we assume that these specifications are clear
enough for the programmer to complete the implementation, we can use the ADT's operations in
a program without knowing the details of the implementation. That is, we do not need to know
how the programmer implemented the list to be able to use it. We only need to know what the
ADT list does.
This section assumes that we have an implementation for the list and demonstrates how
we can use a list in our program.The examples here can be part of a program that tests your
implementation.
VideoNote
Using the ADT list
12.8
Example. Imagine that we are organizing a local road race. Our job is to note the order in which the
runners finish the race. Since each runner wears a distinct identifying number, we can add each run-
ner's number to the end of a list as the runners cross the finish line. Figure 12-3 illustrates such a list.
 
 
Search WWH ::




Custom Search