Java Reference
In-Depth Information
isRemoveOrSetLegal =
false
;
nextIndex++;
ArrayListWithListIterator.
this
.add(nextIndex, newEntry);
}
// end add
FIGURE 15-12
The array of list entries and
nextIndex
(a) just before the call to
add
; (b) just after the call to
add
(a) Before
add
is called
(b) After
add("Ben")
is called
Art
Art
Bart
Bart
Iterator cursor
Ben
Chris
Added entry
nextIndex
= 2
Iterator cursor
Chris
Deb
nextIndex
= 3
Deb
Elly
Elly
15.49
The method
remove
. The logic for the
remove
method when a call to
next
precedes its call is
somewhat like the logic for the
remove
method in the interface
Iterator
, which you saw in
Segment 15.28. Recall that Figure 15-8 illustrated the array of list entries and the index
nextIndex
before and after the calls to
next
and
remove
. Figure 15-13 provides a similar illustration, showing
what happens when a call to
previous
precedes the call to
remove
. In Part
b
,
previous
returns a
reference to the previous entry—
Bart
—in the iteration and decrements
nextIndex
. The method
remove
must remove this entry from the list. Notice that
nextIndex
is now 1 smaller than the posi-
tion number of the list entry that must be removed. After the entry
Bart
has been removed in
Figure 15-13c, the next entry—
Chris
—moves to the next lower-numbered position in the array.
Thus,
nextIndex
remains the index of the next entry in the iteration and so is unchanged.
FIGURE 15-13
The array of list entries and
nextIndex
(a) just before the call to
previous
; (b) just after the call to
previous
but before the call
to
remove
; (c) after the call to
remove
(a) Before
previous()
(b)
previous()
returns
Bart
(c)
remove()
removes
Bart
Art
Art
Art
Iterator cursor
Iterator cursor
nextIndex
= 1
Bart
Chris
Bart
nextIndex
= 1
Iterator cursor
Chris
Deb
Chris
nextIndex
= 2
Deb
Elly
Deb
Elly
Elly