1. /*
  2. * @(#)ListIterator.java 1.23 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. *
  10. * An iterator for lists that allows the programmer
  11. * to traverse the list in either direction, modify
  12. * the list during iteration, and obtain the iterator's
  13. * current position in the list. A <TT>ListIterator</TT>
  14. * has no current element; its <I>cursor position</I> always
  15. * lies between the element that would be returned by a call
  16. * to <TT>previous()</TT> and the element that would be
  17. * returned by a call to <TT>next()</TT>. In a list of
  18. * length <TT>n</TT>, there are <TT>n+1</TT> valid
  19. * index values, from <TT>0</TT> to <TT>n</TT>, inclusive.
  20. * <PRE>
  21. *
  22. * Element(0) Element(1) Element(2) ... Element(n)
  23. * ^ ^ ^ ^ ^
  24. * Index: 0 1 2 3 n+1
  25. *
  26. * </PRE>
  27. * <P>
  28. * Note that the {@link #remove} and {@link #set(Object)} methods are
  29. * <i>not</i> defined in terms of the cursor position; they are defined to
  30. * operate on the last element returned by a call to {@link #next} or {@link
  31. * #previous()}.
  32. * <P>
  33. * This interface is a member of the
  34. * <a href="{@docRoot}/../guide/collections/index.html">
  35. * Java Collections Framework</a>.
  36. *
  37. * @author Josh Bloch
  38. * @version 1.23, 12/19/03
  39. * @see Collection
  40. * @see List
  41. * @see Iterator
  42. * @see Enumeration
  43. * @since 1.2
  44. */
  45. public interface ListIterator<E> extends Iterator<E> {
  46. // Query Operations
  47. /**
  48. * Returns <tt>true</tt> if this list iterator has more elements when
  49. * traversing the list in the forward direction. (In other words, returns
  50. * <tt>true</tt> if <tt>next</tt> would return an element rather than
  51. * throwing an exception.)
  52. *
  53. * @return <tt>true</tt> if the list iterator has more elements when
  54. * traversing the list in the forward direction.
  55. */
  56. boolean hasNext();
  57. /**
  58. * Returns the next element in the list. This method may be called
  59. * repeatedly to iterate through the list, or intermixed with calls to
  60. * <tt>previous</tt> to go back and forth. (Note that alternating calls
  61. * to <tt>next</tt> and <tt>previous</tt> will return the same element
  62. * repeatedly.)
  63. *
  64. * @return the next element in the list.
  65. * @exception NoSuchElementException if the iteration has no next element.
  66. */
  67. E next();
  68. /**
  69. * Returns <tt>true</tt> if this list iterator has more elements when
  70. * traversing the list in the reverse direction. (In other words, returns
  71. * <tt>true</tt> if <tt>previous</tt> would return an element rather than
  72. * throwing an exception.)
  73. *
  74. * @return <tt>true</tt> if the list iterator has more elements when
  75. * traversing the list in the reverse direction.
  76. */
  77. boolean hasPrevious();
  78. /**
  79. * Returns the previous element in the list. This method may be called
  80. * repeatedly to iterate through the list backwards, or intermixed with
  81. * calls to <tt>next</tt> to go back and forth. (Note that alternating
  82. * calls to <tt>next</tt> and <tt>previous</tt> will return the same
  83. * element repeatedly.)
  84. *
  85. * @return the previous element in the list.
  86. *
  87. * @exception NoSuchElementException if the iteration has no previous
  88. * element.
  89. */
  90. E previous();
  91. /**
  92. * Returns the index of the element that would be returned by a subsequent
  93. * call to <tt>next</tt>. (Returns list size if the list iterator is at the
  94. * end of the list.)
  95. *
  96. * @return the index of the element that would be returned by a subsequent
  97. * call to <tt>next</tt>, or list size if list iterator is at end
  98. * of list.
  99. */
  100. int nextIndex();
  101. /**
  102. * Returns the index of the element that would be returned by a subsequent
  103. * call to <tt>previous</tt>. (Returns -1 if the list iterator is at the
  104. * beginning of the list.)
  105. *
  106. * @return the index of the element that would be returned by a subsequent
  107. * call to <tt>previous</tt>, or -1 if list iterator is at
  108. * beginning of list.
  109. */
  110. int previousIndex();
  111. // Modification Operations
  112. /**
  113. * Removes from the list the last element that was returned by
  114. * <tt>next</tt> or <tt>previous</tt> (optional operation). This call can
  115. * only be made once per call to <tt>next</tt> or <tt>previous</tt>. It
  116. * can be made only if <tt>ListIterator.add</tt> has not been called after
  117. * the last call to <tt>next</tt> or <tt>previous</tt>.
  118. *
  119. * @exception UnsupportedOperationException if the <tt>remove</tt>
  120. * operation is not supported by this list iterator.
  121. * @exception IllegalStateException neither <tt>next</tt> nor
  122. * <tt>previous</tt> have been called, or <tt>remove</tt> or
  123. * <tt>add</tt> have been called after the last call to *
  124. * <tt>next</tt> or <tt>previous</tt>.
  125. */
  126. void remove();
  127. /**
  128. * Replaces the last element returned by <tt>next</tt> or
  129. * <tt>previous</tt> with the specified element (optional operation).
  130. * This call can be made only if neither <tt>ListIterator.remove</tt> nor
  131. * <tt>ListIterator.add</tt> have been called after the last call to
  132. * <tt>next</tt> or <tt>previous</tt>.
  133. *
  134. * @param o the element with which to replace the last element returned by
  135. * <tt>next</tt> or <tt>previous</tt>.
  136. * @exception UnsupportedOperationException if the <tt>set</tt> operation
  137. * is not supported by this list iterator.
  138. * @exception ClassCastException if the class of the specified element
  139. * prevents it from being added to this list.
  140. * @exception IllegalArgumentException if some aspect of the specified
  141. * element prevents it from being added to this list.
  142. * @exception IllegalStateException if neither <tt>next</tt> nor
  143. * <tt>previous</tt> have been called, or <tt>remove</tt> or
  144. * <tt>add</tt> have been called after the last call to
  145. * <tt>next</tt> or <tt>previous</tt>.
  146. */
  147. void set(E o);
  148. /**
  149. * Inserts the specified element into the list (optional operation). The
  150. * element is inserted immediately before the next element that would be
  151. * returned by <tt>next</tt>, if any, and after the next element that
  152. * would be returned by <tt>previous</tt>, if any. (If the list contains
  153. * no elements, the new element becomes the sole element on the list.)
  154. * The new element is inserted before the implicit cursor: a subsequent
  155. * call to <tt>next</tt> would be unaffected, and a subsequent call to
  156. * <tt>previous</tt> would return the new element. (This call increases
  157. * by one the value that would be returned by a call to <tt>nextIndex</tt>
  158. * or <tt>previousIndex</tt>.)
  159. *
  160. * @param o the element to insert.
  161. * @exception UnsupportedOperationException if the <tt>add</tt> method is
  162. * not supported by this list iterator.
  163. *
  164. * @exception ClassCastException if the class of the specified element
  165. * prevents it from being added to this list.
  166. *
  167. * @exception IllegalArgumentException if some aspect of this element
  168. * prevents it from being added to this list.
  169. */
  170. void add(E o);
  171. }