|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
For the purposes of the AP subset (AB only) this
interface is an Iterator
with two additional
methods for changing the collection of elements
being iterated over.
Conceptually a ListIterator
traverses a list
as though there is a header node. Thus calling next
is not valid for a ListIterator
constructed
from an empty list which conceptually has a header node
but no node after the header node. However, calling
add
is valid for a ListIterator
constructed from an empty list and will result in
adding a new node after the header node, though this new value
will not affect calls to next
or to
hasNext
for the ListIterator
.
The code below shows how to change the list
("hello", "goodbye")to the list
("hello", "hello 2", "goodbye", "goodbye 2")using a
ListIterator
.
// double-up a list of strings public static void doubleUp(List list) { ListIterator it = list.listIterator(); while (it.hasNext()) { String s = (String) it.next(); it.add(s + " 2"); } }The code below changes the initially empty list being iterated over to a list with the string
"first"
, but does not
print anything.
ArrayList list = new ArrayList(); ListIterator it = list.listIterator(); it.add("first"); // add a first element while (it.hasNext()) { // but nothint printed System.out.println(it.next()); // by this loop }The code below uses
set
to "double" every string in a list, e.g., to change the list
("one", "two")
to ("oneone", "twotwo")
.
it = list.listIterator(); while (it.hasNext()) { String s = (String) it.next(); it.set(s+s); }
Method Summary | |
void |
add(java.lang.Object o)
Inserts the argument into the list being iterated over. |
void |
set(java.lang.Object o)
Replaces the last element returned by next by
the argument. |
Methods inherited from interface ap.java.util.Iterator |
hasNext, next, remove |
Method Detail |
public void add(java.lang.Object o)
next
if there is any
such element, however the value returned by a subsequent
call of next
is not affected.
o
- is the Object inserted into the collection being
iterated over
java.lang.UnsupportedOperationException
- if add
is not supported by this list iteratorpublic void set(java.lang.Object o)
next
by
the argument. It is not valid to call set
if
either add
or remove
have been called
after the last call to next
.
o
- is the object that replaces the last element returned
by next
IllegalStateException
- if next
has not been called or if remove
or
add
have been called
after the last call of next
java.lang.UnsupportedOperationException
- if set
is not supported by this list iterator
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |