Java Reference
In-Depth Information
nameIterator.next();
nameIterator.remove();
System.out.println(nameIterator.next());
displayList(nameList);
System.out.println(nameIterator.next());
System.out.println(nameIterator.next());
3.
Suppose that
nameList
is a list of at least one string and that
nameIterator
is defined as follows:
Iterator<String> nameIterator = nameList.getIterator();
Write Java statements that use
nameIterator
to display only the last string in the list.
4.
Given
nameList
and
nameIterator
as described in Exercise 3, write statements that display all strings in the list
from last to first.
5.
Given
nameList
and
nameIterator
as described in Exercise 3, write statements that use
nameIterator
to remove
all the entries from the list.
6.
Given
nameList
and
nameIterator
as described in Exercise 3, write statements that remove all occurrences of the
string
CANCEL
from the list.
7.
Given
nameList
and
nameIterator
as described in Exercise 3, write statements that remove any duplicates in the list.
8.
Given
nameList
and
nameIterator
as described in Exercise 3, write statements that count the number of times
each string occurs in the list, without altering the list and without repeating the computations.
9.
Suppose that
aList
and
bList
are instances of
java.util.ArrayList
. Use two iterators to find and display all the
objects that are common to both lists. Do not alter the contents of either list.
10.
Assume that
aList
and
bList
are instances of
java.util.ArrayList
that contain
Comparable
objects in order
from smallest to largest. Use two iterators to move the objects from
bList
to the appropriate locations in
aList
.
When you are done, the objects in
aList
should be in order, and
bList
should be empty.
11.
Revise the class
SeparateIterator
outlined in Segment 15.12 so that it does not support a remove operation.
Simplify the class as much as possible.
12.
Imagine a class that implements the interface
ListWithIteratorInterface
, as given in Listing 15-11 of
Segment 15.51. Suppose that
aList
is an instance of this class and contains
Comparable
objects in no
particular order. Using an iterator, implement the following two methods within the class:
a.
getMin
returns the smallest object in the list
b.
removeMin
removes and returns the smallest object in the list
13.
Repeat the previous exercise, but use a for-each loop instead of an iterator.
14.
Suppose that
nameList
is a list that contains the following strings:
Kyle, Cathy, Sam, Austin, Sara.
What output is
produced by the following sequence of statements?
ListIterator<String> nameIterator = nameList.getIterator();
System.out.println(nameIterator.next());
nameIterator.next();
nameIterator.next();
System.out.println(nameIterator.next());
nameIterator.set("Brittany");
nameIterator.previous();
nameIterator.remove();
System.out.println(nameIterator.next());
displayList(nameList);