|
Bad News Bearers Lab | |||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
java.lang.Object | +--CircularLLIterator
The CircularLLIterator class provides an iterator through
a linear LinkedList object that makes it appear to be
circular. The element after the last element in the list,
as returned by next, is the first element in
the list. The element before the first element in the list,
as returned by previous, is the last element
in the list.
In a circular iteration, the methods hasNext
and hasPrevious will always return
true unless the list is empty. This means
that the usual iteration pattern of
while (iterator.hasNext())
<do something>
will result in an infinite loop. To iterate once through
the elements of the list, use the list iterator returned by
the linked list's listIterator method or get
the size of the list iterate through that many times. (The
latter technique will not work if you are adding or removing
elements as you iterate.)
LinkedList| Constructor Summary | |
CircularLLIterator(java.util.LinkedList list)
Constructs a circular iterator for the specified list. |
|
| Method Summary | |
void |
add(java.lang.Object obj)
Inserts the specified element into the list. |
boolean |
hasNext()
Returns true if this list iterator has more elements
when traversing the list in the forward direction. |
boolean |
hasPrevious()
Returns true if this list iterator has more elements
when traversing the list in the reverse direction. |
static void |
main(java.lang.String[] args)
Runs test suite for the CircularLLIterator class. |
java.lang.Object |
next()
Returns the next element in the list. |
int |
nextIndex()
Returns the index of the element that would be returned by a subsequent call to next. |
java.lang.Object |
previous()
Returns the previous element in the list. |
int |
previousIndex()
Returns the index of the element that would be returned by a subsequent call to previous. |
void |
remove()
Removes from the list the last element that was returned by next or previous. |
void |
set(java.lang.Object obj)
Replaces the last element returned by next or
previous with the specified element. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public CircularLLIterator(java.util.LinkedList list)
list - the linked list through which to iterate| Method Detail |
public boolean hasNext()
true if this list iterator has more elements
when traversing the list in the forward direction. (In other words,
returns true if next would return an
element rather than throwing an exception.) Since this is a
circular iterator, returns true unless the list is
empty.hasNext in interface java.util.ListIteratortrue if the list iterator has more elements
when traversing the list in the forward directionpublic boolean hasPrevious()
true if this list iterator has more elements
when traversing the list in the reverse direction. (In other words,
returns true if previous would return an
element rather than throwing an exception.) Since this is a
circular iterator, returns true unless the list is
empty.hasPrevious in interface java.util.ListIteratortrue if the list iterator has more elements
when traversing the list in the reverse directionpublic int nextIndex()
next. (Returns list size if the
list is empty.)nextIndex in interface java.util.ListIteratorpublic int previousIndex()
previous. (Returns -1 if the
list is empty.)previousIndex in interface java.util.ListIteratorpublic java.lang.Object next()
previous to go back and forth. (Note that
alternating calls to next and previous
will return the same element repeatedly.)next in interface java.util.ListIteratorNoSuchElementException - if the list is emptypublic java.lang.Object previous()
next to go back and forth. (Note that
alternating calls to next and previous
will return the same element repeatedly.)previous in interface java.util.ListIteratorNoSuchElementException - if the list is emptypublic void add(java.lang.Object obj)
next, if any, and after the next element that would be
returned by previous, if any. (If the list contains no
elements, the new element becomes the sole element on the list.)
The new element is inserted before the implicit cursor: a subsequent
call to next would be unaffected, and a subsequent call
to previous would return the new element. (This call
increases by one the value that would be returned by a call to
nextIndex or previousIndex.)add in interface java.util.ListIteratorobj - the element to insertpublic void remove()
next or previous. This call can only
be made once per call to next or previous.
It can be made only if ListIterator.add has not been
called after the last call to next or
previous.remove in interface java.util.ListIteratorIllegalStateException - neither next nor
previous have been called, or
remove or add have been called
after the last call to next or
previouspublic void set(java.lang.Object obj)
next or
previous with the specified element. This call
can be made only if neither ListIterator.remove
nor ListIterator.add have been called after the
last call to next or previous.set in interface java.util.ListIteratorobj - the element with which to replace the last element
returned by next or previouspublic static void main(java.lang.String[] args)
CircularLLIterator class.args - standard parameter; not used
|
Bad News Bearers Lab | |||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||