java.util

Interface ListIterator<E>

All Superinterfaces:
Iterator<E>
Known Subinterfaces:
ResettableListIterator
Known Implementing Classes:
AbstractLinkedList.LinkedListIterator, AbstractLinkedList.LinkedSubListIterator, AbstractListIteratorDecorator, ArrayListIterator, ByteListIteratorListIterator, CharListIteratorListIterator, CursorableLinkedList.Cursor, CursorableLinkedList.Cursor, CursorableLinkedList.SubCursor, CursorableLinkedList.Cursor, DoubleListIteratorListIterator, EmptyListIterator, FilterListIterator, FloatListIteratorListIterator, IntListIteratorListIterator, ListIteratorWrapper, LongListIteratorListIterator, LoopingListIterator, ObjectArrayListIterator, PredicatedList.PredicatedListIterator, ProxyListIterator, ReverseListIterator, ShortListIteratorListIterator, SingletonListIterator, StrTokenizer, TransformedList.TransformedListIterator, UnmodifiableListIterator

public interface ListIterator<E>
extends Iterator<E>

An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.

          Element(0)   Element(1)   Element(2)   ... Element(n)   
        ^            ^            ^            ^               ^
 Index: 0            1            2            3               n+1

 

Note that the remove() and set(Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous().

This interface is a member of the ../../../guide/collections/index.html"> Java Collections Framework.

Since:
1.2
See Also:
Collection, List, Iterator, Enumeration

Method Summary

void
add(E o)
Inserts the specified element into the list (optional operation).
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.
E
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.
E
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 (optional operation).
void
set(E o)
Replaces the last element returned by next or previous with the specified element (optional operation).

Methods inherited from interface java.util.Iterator<E>

hasNext, next, remove

Method Details

add

public void add(E o)
Inserts the specified element into the list (optional operation). The element is inserted immediately before the next element that would be returned by 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.)
Parameters:
o - the element to insert.
Throws:
UnsupportedOperationException - if the add method is not supported by this list iterator.
ClassCastException - if the class of the specified element prevents it from being added to this list.
IllegalArgumentException - if some aspect of this element prevents it from being added to this list.
Usages and Demos :

View More Examples of add(E o)
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public void add(Object o) {
  12:     iterator.add(adapter.toPersistentObject(o));

View Full Code Here
   1:       a.indexOf(s[5]));
   2:     ListIterator lit = a.listIterator(a.size());
   3:     while(lit.hasPrevious())
   4:       System.out.print(lit.previous());
   5:     System.out.println();

View Full Code Here
   1:         for (int i = 0; i < els.length; i++) {
   2:             list.add(els[i]);
   3:             assertEquals(i + 1, list.size());
   4:         ...
   5: 
   6:         ListIterator it = list.listIterator();
   7:         it.next();
   8:         ...
   9:         assertEquals(new Integer(33), list.get(0));
  10:         it.add("44");
  11:         assertEquals(new Integer(44), list.get(1));
  12:         ...
  13:         List adds = new ArrayList();
  14:         adds.add("1");

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:         for (int i = 0; i < els.length; i++) {
   6:             list.add(els[i]);
   7:             assertEquals(i + 1, list.size());
   8:         ...
   9:         
  10:         ListIterator it = list.listIterator();
  11:         it.next();
  12:         ...
  13:         assertEquals(new Integer(33), list.get(0));
  14:         it.add("44");

View Full Code Here
   1: import java.util.Iterator;
   2: import java.util.ListIterator;
   3: import java.util.NoSuchElementException;
   4:         ...
   5: 
   6:     public abstract ListIterator makeEmptyListIterator();
   7: 
   8:         ...
   9:     public abstract ListIterator makeFullListIterator();
  10: 
  11:         ...
  12:         while (it.hasNext()) {
  13:             list.add(it.next());

View Full Code Here

hasNext

public boolean hasNext()
Returns 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.)
Specified by:
hasNext in interface Iterator<E>
Returns:
true if the list iterator has more elements when traversing the list in the forward direction.
Usages and Demos :

View More Examples of hasNext()
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public boolean hasNext() {
  12:     return iterator.hasNext();

View Full Code Here
   1: 
   2:             for (ListIterator iter = all.listIterator() ;
   3:         ...
   4:                  iter.hasNext() ; ) {
   5:                 System.out.println(iter.next());
   6:             }
   7:         } finally {

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized boolean hasNext() {
  14:         return iterator.hasNext();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public boolean hasNext() {
  12:             return  iterator.hasNext();

View Full Code Here
   1:         LinkedList locators = (LinkedList) servicesRegistry.get( serviceName );
   2:         for ( ListIterator li = locators.listIterator();
   3:         ...
   4:           li.hasNext(); )  {
   5:         ServiceLocation  sl = ((ServiceLocation) li.next());
   6:         if ( sl.isSameIP(ip) ) {
   7:             li.remove();

View Full Code Here

hasPrevious

public boolean hasPrevious()
Returns 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.)
Returns:
true if the list iterator has more elements when traversing the list in the reverse direction.
Usages and Demos :

View More Examples of hasPrevious()
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected Iterator dispatchOrder( List invokablesList ) {
   6:         final ListIterator i = invokablesList.listIterator(this.invokables.size());
   7:         return new Iterator() {
   8:         ...
   9:             public boolean hasNext() {
  10:                 return i.hasPrevious();
  11:             }

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5: 
   6:     ListIterator m_iter;
   7: 
   8:         ...
   9:     public ReverseListIterator(List list) {
  10:         m_iter = list.listIterator();
  11:         while ( m_iter.hasNext() ) { m_iter.next(); }    
  12:         ...
  13:     public boolean hasNext() {
  14:         return m_iter.hasPrevious();

View Full Code Here
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public boolean hasPrevious() {
  12:     return iterator.hasPrevious();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized boolean hasPrevious() {
  14:         return iterator.hasPrevious();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public boolean hasPrevious() {
  12:             return  iterator.hasPrevious();

View Full Code Here

next

public E next()
Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
Specified by:
next in interface Iterator<E>
Returns:
the next element in the list.
Throws:
NoSuchElementException - if the iteration has no next element.
Usages and Demos :

View More Examples of next()
   1: import java.util.ArrayList;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:       if (index > -1) {
   6:         ListIterator li = this.listIterator(index);
   7:         ...
   8:         li.next();
   9:         while (li.hasNext()) {
  10:         ...
  11:           String n = (String) li.next();

View Full Code Here
   1:           while(it.hasNext())
   2:             it.next();
   3:         }
   4:         ...
   5:         String s = "test";
   6:         ListIterator it = a.listIterator(half);
   7:         for(int i = 0; i < size * 10; i++)
   8:         ...
   9:       void test(List a) {
  10:         ListIterator it = a.listIterator(3);
  11:         while(it.hasNext()) {
  12:         ...
  13:           it.next();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized Object next() {
  14:         return iterator.next();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public Object next() {
  12:             return iterator.next();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: import java.util.NoSuchElementException;
   4:         ...
   5:     
   6:     public ListIterator openIterator( int index ) {
   7:         return new SubResourceListIterator( index );
   8:         ...
   9:     
  10:     private class SubResourceListIterator implements ListIterator {
  11:         ListIterator delegate;
  12:         ...
  13:                 throw new NoSuchElementException();
  14:             return delegate.next();

View Full Code Here

nextIndex

public int nextIndex()
Returns the index of the element that would be returned by a subsequent call to next. (Returns list size if the list iterator is at the end of the list.)
Returns:
the index of the element that would be returned by a subsequent call to next, or list size if list iterator is at end of list.
Usages and Demos :

View More Examples of nextIndex()
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public int nextIndex() {
  12:     return iterator.nextIndex();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized int nextIndex() {
  14:         return iterator.nextIndex();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public int nextIndex() {
  12:             return iterator.nextIndex();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: import java.util.NoSuchElementException;
   4:         ...
   5:     
   6:     public ListIterator openIterator( int index ) {
   7:         return new SubResourceListIterator( index );
   8:         ...
   9:     
  10:     private class SubResourceListIterator implements ListIterator {
  11:         ListIterator delegate;
  12:         ...
  13:         public boolean hasNext() {
  14:             return delegate.nextIndex() < toIndex;

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5: public class RandomAccessListIterator implements ListIterator {
   6:         ...
   7:     private ListIterator delegate = null;
   8: 
   9:         ...
  10:     public int nextIndex() {
  11:         return delegate.nextIndex();

View Full Code Here

previous

public E previous()
Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
Returns:
the previous element in the list.
Throws:
NoSuchElementException - if the iteration has no previous element.
Usages and Demos :

View More Examples of previous()
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected Iterator dispatchOrder( List invokablesList ) {
   6:         final ListIterator i = invokablesList.listIterator(this.invokables.size());
   7:         return new Iterator() {
   8:         ...
   9:             public Object next() {
  10:                 return i.previous();
  11:             }

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5: 
   6:     ListIterator m_iter;
   7: 
   8:         ...
   9:     public ReverseListIterator(List list) {
  10:         m_iter = list.listIterator();
  11:         while ( m_iter.hasNext() ) { m_iter.next(); }    
  12:         ...
  13:     public Object next() {        
  14:         return m_iter.previous();

View Full Code Here
   1: 
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5: public void undo() {
   6:     ListIterator itr = getCommands().listIterator();
   7:     try {
   8:         ...
   9:         while (itr.hasPrevious()) {
  10:             ((Command) itr.previous()).redo();
  11:         }

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized Object previous() {
  14:         return iterator.previous();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public Object previous() {
  12:             return iterator.previous();

View Full Code Here

previousIndex

public int previousIndex()
Returns the index of the element that would be returned by a subsequent call to previous. (Returns -1 if the list iterator is at the beginning of the list.)
Returns:
the index of the element that would be returned by a subsequent call to previous, or -1 if list iterator is at beginning of list.
Usages and Demos :

View More Examples of previousIndex()
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public int previousIndex() {
  12:     return iterator.previousIndex();

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:     protected List traversalOrderList;
   6:     protected ListIterator iterator;
   7: 
   8:         ...
   9:         traversalOrderList = buildTraversalOrder();
  10:         iterator = traversalOrderList.listIterator();
  11:     }
  12:         ...
  13:     public synchronized int previousIndex() {
  14:         return iterator.previousIndex();

View Full Code Here
   1: 
   2:     protected class BasicIterator implements ListIterator {
   3:         ...
   4:         protected ListIterator iterator;
   5: 
   6:         ...
   7:         protected BasicIterator() {
   8:             this.iterator = AbstractBridgeList.this.listIterator();
   9:         }
  10:         ...
  11:         public int previousIndex() {
  12:             return iterator.previousIndex();

View Full Code Here
   1: import java.util.Iterator;
   2: import java.util.ListIterator;
   3: import org.w3c.dom.DOMException;
   4:         ...
   5:   private int indexOf(NodeImpl node) {
   6:       ListIterator i=this.listIterator();
   7:       while (i.hasNext()) {
   8:         ...
   9:           NodeImpl temp=(NodeImpl)i.next();
  10:         if (temp.getNodeName().compareTo(node.getNodeName())==0) return i.previousIndex();
  11:       }

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: import java.util.NoSuchElementException;
   4:         ...
   5:     
   6:     public ListIterator openIterator( int index ) {
   7:         return new SubResourceListIterator( index );
   8:         ...
   9:     
  10:     private class SubResourceListIterator implements ListIterator {
  11:         ListIterator delegate;
  12:         ...
  13:         public boolean hasPrevious() {
  14:             return delegate.previousIndex() > fromIndex;

View Full Code Here

remove

public void remove()
Removes from the list the last element that was returned by next or previous (optional operation). 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.
Specified by:
remove in interface Iterator<E>
Throws:
UnsupportedOperationException - if the remove operation is not supported by this list iterator.
IllegalStateException - neither next nor previous have been called, or remove or add have been called after the last call to * next or previous.
Usages and Demos :

View More Examples of remove()
   1: 
   2: public class ListIteratorAdapter implements ListIterator {
   3:         ...
   4:     ListIterator iterator;
   5:     AbstractAdapter adapter;
   6:         ...
   7: 
   8:     public ListIteratorAdapter(ListIterator iterator, AbstractAdapter adapter) {
   9:     this.iterator=iterator;
  10:         ...
  11:     public void remove() {
  12:     iterator.remove();

View Full Code Here
   1: import org.mmbase.applications.media.urlcomposers.URLComposer;
   2: import java.util.ListIterator;
   3: import org.mmbase.util.XMLBasicReader;
   4:         ...
   5:     public List filter(List urlcomposers) {
   6:         ListIterator i = urlcomposers.listIterator();
   7:         while (i.hasNext()) {
   8:         ...
   9:             if (! uc.isAvailable()) {
  10:                 i.remove();
  11:             }

View Full Code Here
   1: import java.util.ArrayList;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:       if (index > -1) {
   6:         ListIterator li = this.listIterator(index);
   7:         li.next();
   8:         ...
   9:           String n = (String) li.next();
  10:           li.remove();
  11:         }

View Full Code Here
   1: 
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:         
   6:         ListIterator namespaces = node.additionalNamespaces().listIterator();
   7:         while (namespaces.hasNext()) {
   8:         ...
   9:             if (additionalNamespace.getURI().equals(from.getURI())) {
  10:                 namespaces.remove();
  11:             }

View Full Code Here
   1:         String s = "test";
   2:         ListIterator it = a.listIterator(half);
   3:         for(int i = 0; i < size * 10; i++)
   4:         ...
   5:       void test(List a) {
   6:         ListIterator it = a.listIterator(3);
   7:         while(it.hasNext()) {
   8:         ...
   9:           it.next();
  10:           it.remove();
  11:         }

View Full Code Here

set

public void set(E o)
Replaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither ListIterator.remove nor ListIterator.add have been called after the last call to next or previous.
Parameters:
o - the element with which to replace the last element returned by next or previous.
Throws:
UnsupportedOperationException - if the set operation is not supported by this list iterator.
ClassCastException - if the class of the specified element prevents it from being added to this list.
IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list.
IllegalStateException - if neither next nor previous have been called, or remove or add have been called after the last call to next or previous.
Usages and Demos :

View More Examples of set(E o)
   1: 
   2:     ListIterator iterator = transformedList.listIterator();
   3:     while (iterator.hasNext()) {
   4:         ...
   5:       iterator.set(aTransformer.transform(iterator.next()));
   6:     }
   7:     return transformedList;
   8:   }

View Full Code Here
   1: 
   2:         ListIterator it = list.listIterator();
   3:         it.next();
   4:         ...
   5:         it.set("33");
   6:         assertEquals(new Integer(33), list.get(0));
   7:         it.add("44");
   8:         assertEquals(new Integer(44), list.get(1));

View Full Code Here
   1: import java.util.List;
   2: import java.util.ListIterator;
   3: 
   4:         ...
   5:         
   6:         ListIterator it = list.listIterator();
   7:         it.next();
   8:         ...
   9:         it.set("33");
  10:         assertEquals(new Integer(33), list.get(0));

View Full Code Here
   1: 
   2:     ListIterator it = excepts.listIterator();
   3:     while (it.hasNext()) {
   4:         ...
   5:         it.set(TreeUtilities.listToName((List)it.next()));
   6:     }
   7:     }

View Full Code Here
   1: 
   2:     ListIterator it = excepts.listIterator();
   3:     while (it.hasNext()) {
   4:         ...
   5:         it.set(TreeUtilities.listToName((List)it.next()));
   6:     }
   7:     }

View Full Code Here