Java Reference
In-Depth Information
of the
ArrayBunchList
will go through
set
, which modifies the appropriate
underlying array.
AbstractList
provides
Iterator
and
ListIterator
implementations on top
of the other methods of the class. Because the iteration implementations
use the methods of your underlying subclass of
AbstractList
, the modi-
fying methods of the iteration will properly reflect the modifiability of
your class.
The
Iterator
implementations of
AbstractList
use
get
to read values. As
you will note,
ArrayBunchList
has a
get
that can do some significant work
if the value is stored in one of the later arrays. We can make
get
smarter
than shown here to help with this work, but we can be even more effi-
cient for iteration because it accesses the data sequentially. Here is an
optimized
Iterator
:
private class ABLIterator implements Iterator<E> {
private int off; // offset from start of list
private int array; // array we are currently in
private int pos; // position in current array
ABLIterator() {
off = 0;
array = 0;
pos = 0;
// skip any initial empty arrays (or to end)
for (array = 0; array < arrays.length; array++)
if (arrays[array].length > 0)
break;
}
public boolean hasNext() {
return off + pos < size();
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
E ret = arrays[array][pos++];