Java Reference
In-Depth Information
removeItems
(lines 64-68) to remove the range of elements starting at index
4
up to, but
not including, index
7
of the list. Line 37 calls method
printReversedList
(lines 71-80)
to print the list in reverse order.
Method
convertToUppercaseStrings
Method
convertToUppercaseStrings
(lines 52-61) changes lowercase
String
elements
in its
List
argument to uppercase
String
s. Line 54 calls
List
method
listIterator
to
get the
List
's
bidirectional iterator
(i.e., one that can traverse a
List
backward
or
for-
ward
).
ListIterator
is also a generic class. In this example, the
ListIterator
references
String
objects, because method
listIterator
is called on a
List
of
String
s. Line 56 calls
method
hasNext
to determine whether the
List
contains another element
. Line 58 gets the
next
String
in the
List
. Line 59 calls
String
method
toUpperCase
to get an uppercase
version of the
String
and calls
ListIterator
method
set
to replace the current
String
to which
iterator
refers with the
String
returned by method
toUpperCase
. Like method
toUpperCase
,
String
method
toLowerCase
returns a lowercase version of the
String
.
Method
removeItems
Method
removeItems
(lines 64-68)
removes a range of items
from the list. Line 67 calls
List
method
subList
to obtain a portion of the
List
(called a
sublist
). This is called a
range-view
method
, which enables the program to view a portion of the list. The sublist
is simply a view into the
List
on which
subList
is called. Method
subList
takes as argu-
ments the beginning and ending index for the sublist. The ending index is
not
part of the
range of the sublist. In this example, line 35 passes
4
for the beginning index and
7
for the
ending index to
subList
. The sublist returned is the set of elements with indices
4
through
6
. Next, the program calls
List
method
clear
on the sublist to remove the elements of
the sublist from the
List
. Any changes made to a sublist are also made to the original
List
.
Method
printReversedList
Method
printReversedList
(lines 71-80) prints the list backward. Line 73 calls
List
method
listIterator
with the starting position as an argument (in our case, the last ele-
ment in the list) to get a
bidirectional iterator
for the list.
List
method
size
returns the
number of items in the
List
. The
while
condition (line 78) calls
ListIterator
's
hasPre-
vious
method
to determine whether there are more elements while traversing the list
back-
ward
. Line 79 calls
ListIterator
's
previous
method
to get the previous element from
the list and outputs it to the standard output stream.
Views into Collections and
Arrays
Method
asList
Class
Arrays
provides
static
method
asList
to
view
an array (sometimes called the
back-
ing array
) as a
List
collection. A
List
view allows you to manipulate the array as if it were
a list. This is useful for adding the elements in an array to a collection and for sorting array
elements. The next example demonstrates how to create a
LinkedList
with a
List
view
of an array, because we cannot pass the array to a
LinkedList
constructor. Sorting array
elements with a
List
view is demonstrated in Fig. 16.7. Any modifications made through
the
List
view change the array, and any modifications made to the array change the
List
view. The only operation permitted on the view returned by
asList
is
set
, which changes
the value of the view and the backing array. Any other attempts to change the view (such
as adding or removing elements) result in an
UnsupportedOperationException
.