Java Reference
In-Depth Information
the element that would be returned by a call to
previous()
and the element that
wouldbereturnedbyacallto
next()
.Alistiteratorforalistoflength
n
has
n
+1pos-
sible cursor positions, as illustrated by each caret (
^
) as shown here:
Element(0)
Element(1)
Ele-
ment(2)
... Element(n-1)
cursor
posi-
tions: ^
^
^
^
The
remove()
and
set()
methodsarenotdefinedintermsofthecursorposition;
theyaredefinedtooperateonthelastelementreturnedbyacallto
next()
or
pre-
vious()
.
Note
Youcanmixcallsto
next()
and
previous()
aslongasyouarecareful.
Keep inmind that the first call to
previous()
returns the same element asthe last
callto
next()
.Furthermore,thefirstcallto
next()
followingasequenceofcallsto
previous()
returns the same element as the last call to
previous()
.
whichisalistthatisbackedbyanotherlist.Changesthataremadetotheviewarere-
flectedinthisbackinglist.Theviewcancovertheentirelistor,as
subList()
'sname
implies, only part of the list.
The
subList()
method is useful for performing
range-view
operations over a
list in a compact manner. For example,
list.subList(fromIndex, toIn-
dex).clear();
removesarangeofelementsfrom
list
wherethefirstelementis
located at
fromIndex
and the last element is located at
toIndex-1
.
Caution
Aview'smeaningbecomesundefinedwhenchangesaremadetotheback-
inglist.Therefore,youshouldonlyuse
subList()
temporarily,wheneveryouneed
to perform a sequence of range operations on the backing list.
ArrayList
The
ArrayList
classprovidesalistimplementationthatisbasedonaninternalarray
(seeChapters1and2).Asaresult,accesstothelist'selementsisfast.However,because
elementsmustbemovedtoopenaspaceforinsertionortocloseaspaceafterdeletion,
insertions and deletions of elements is slow.
ArrayList
supplies three constructors: