Java Reference
In-Depth Information
5.
False. When the list is empty, both nextPosition and list.getLength() are zero.
6.
Linked. The particular implementation of the list affects the amount of work that the method getEntry must per-
form. For an array-based implementation, getEntry accesses the required entry directly and immediately. For a
linked implementation, getEntry must traverse a chain of nodes to find the desired entry. This takes more time to
accomplish than accessing an array entry.
7.
False. When the list is empty, firstNode , and therefore nextNode , is null .
8.
Create the iterators by writing
Iterator<String> nameIterator = nameList.getIterator();
Iterator<String> countingIterator = nameList.getIterator();
9.
public static void displayListQ9(ListWithIteratorInterface<String> list)
{
int numberOfEntries = list.getLength();
System.out.println("The list contains " + numberOfEntries +
" entries, as follows:");
Iterator<String> traverser = list.getIterator();
int position = 0;
while (traverser.hasNext())
{
position++;
System.out.println(traverser.next() + " is entry " + position);
} // end while
System.out.println();
} // end displayListQ9
10.
a. Deb.
b. Deb.
11.
Originally, nextIndex is the index of the next entry that next will return. The change makes nextIndex the index
of the last entry that next returned. Thus, the following changes are needed:
hasNext should compare nextIndex to numberOfEntries - 1 instead of numberOfEntries
next should increment nextIndex before accessing list[nextIndex]
remove should remove the entry at nextIndex + 1
12.
public static void displayListQ12(ListWithTraversal<String> list)
{
int numberOfEntries = list.getLength();
System.out.println("The list contains " + numberOfEntries +
" entries, as follows:");
list.resetTraversal();
int position = 0;
while (list.hasNext())
{
position++;
System.out.println(list.next() + " is entry " + position);
} // end while
System.out.println();
} // end displayListQ12
A disadvantage is that if you have an iteration in progress, and you pause it to call displayListQ12 , you will not
be able to resume your iteration.
 
Search WWH ::




Custom Search