Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.

1 /*2 * @(#)ListIterator.java 1.23 03/12/193 *4 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.6 */7 8 package java.util;9 10 /**11 * 12 * An iterator for lists that allows the programmer 13 * to traverse the list in either direction, modify 14 * the list during iteration, and obtain the iterator's 15 * current position in the list. A <TT>ListIterator</TT> 16 * has no current element; its <I>cursor position</I> always 17 * lies between the element that would be returned by a call 18 * to <TT>previous()</TT> and the element that would be 19 * returned by a call to <TT>next()</TT>. In a list of 20 * length <TT>n</TT>, there are <TT>n+1</TT> valid 21 * index values, from <TT>0</TT> to <TT>n</TT>, inclusive. 22 * <PRE>23 *24 * Element(0) Element(1) Element(2) ... Element(n) 25 * ^ ^ ^ ^ ^26 * Index: 0 1 2 3 n+127 *28 * </PRE>29 * <P>30 * Note that the {@link #remove} and {@link #set(Object)} methods are31 * <i>not</i> defined in terms of the cursor position; they are defined to32 * operate on the last element returned by a call to {@link #next} or {@link33 * #previous()}.34 * <P>35 * This interface is a member of the 36 * <a HREF="{@docRoot}/../guide/collections/index.html">37 * Java Collections Framework</a>.38 *39 * @author Josh Bloch40 * @version 1.23, 12/19/0341 * @see Collection42 * @see List43 * @see Iterator44 * @see Enumeration45 * @since 1.246 */47 publicinterface ListIterator<E> extendsIterator<E> {48 // Query Operations49 50 /**51 * Returns <tt>true</tt> if this list iterator has more elements when52 * traversing the list in the forward direction. (In other words, returns53 * <tt>true</tt> if <tt>next</tt> would return an element rather than54 * throwing an exception.)55 *56 * @return <tt>true</tt> if the list iterator has more elements when57 * traversing the list in the forward direction.58 */59 boolean hasNext();60 61 /**62 * Returns the next element in the list. This method may be called63 * repeatedly to iterate through the list, or intermixed with calls to64 * <tt>previous</tt> to go back and forth. (Note that alternating calls65 * to <tt>next</tt> and <tt>previous</tt> will return the same element66 * repeatedly.)67 *68 * @return the next element in the list.69 * @exception NoSuchElementException if the iteration has no next element.70 */71 E next();72 73 /**74 * Returns <tt>true</tt> if this list iterator has more elements when75 * traversing the list in the reverse direction. (In other words, returns76 * <tt>true</tt> if <tt>previous</tt> would return an element rather than77 * throwing an exception.)78 *79 * @return <tt>true</tt> if the list iterator has more elements when80 * traversing the list in the reverse direction.81 */82 boolean hasPrevious();83 84 /**85 * Returns the previous element in the list. This method may be called86 * repeatedly to iterate through the list backwards, or intermixed with87 * calls to <tt>next</tt> to go back and forth. (Note that alternating88 * calls to <tt>next</tt> and <tt>previous</tt> will return the same89 * element repeatedly.)90 *91 * @return the previous element in the list.92 * 93 * @exception NoSuchElementException if the iteration has no previous94 * element.95 */96 E previous();97 98 /**99 * Returns the index of the element that would be returned by a subsequent100 * call to <tt>next</tt>. (Returns list size if the list iterator is at the101 * end of the list.)102 *103 * @return the index of the element that would be returned by a subsequent104 * call to <tt>next</tt>, or list size if list iterator is at end105 * of list. 106 */107 int nextIndex();108 109 /**110 * Returns the index of the element that would be returned by a subsequent111 * call to <tt>previous</tt>. (Returns -1 if the list iterator is at the112 * beginning of the list.)113 *114 * @return the index of the element that would be returned by a subsequent115 * call to <tt>previous</tt>, or -1 if list iterator is at116 * beginning of list.117 */118 int previousIndex();119 120 121 // Modification Operations122 123 /**124 * Removes from the list the last element that was returned by125 * <tt>next</tt> or <tt>previous</tt> (optional operation). This call can126 * only be made once per call to <tt>next</tt> or <tt>previous</tt>. It127 * can be made only if <tt>ListIterator.add</tt> has not been called after128 * the last call to <tt>next</tt> or <tt>previous</tt>.129 *130 * @exception UnsupportedOperationException if the <tt>remove</tt>131 * operation is not supported by this list iterator.132 * @exception IllegalStateException neither <tt>next</tt> nor133 * <tt>previous</tt> have been called, or <tt>remove</tt> or134 * <tt>add</tt> have been called after the last call to *135 * <tt>next</tt> or <tt>previous</tt>.136 */137 void remove();138 139 /**140 * Replaces the last element returned by <tt>next</tt> or141 * <tt>previous</tt> with the specified element (optional operation).142 * This call can be made only if neither <tt>ListIterator.remove</tt> nor143 * <tt>ListIterator.add</tt> have been called after the last call to144 * <tt>next</tt> or <tt>previous</tt>.145 *146 * @param o the element with which to replace the last element returned by147 * <tt>next</tt> or <tt>previous</tt>.148 * @exception UnsupportedOperationException if the <tt>set</tt> operation149 * is not supported by this list iterator.150 * @exception ClassCastException if the class of the specified element151 * prevents it from being added to this list.152 * @exception IllegalArgumentException if some aspect of the specified153 * element prevents it from being added to this list.154 * @exception IllegalStateException if neither <tt>next</tt> nor155 * <tt>previous</tt> have been called, or <tt>remove</tt> or156 * <tt>add</tt> have been called after the last call to157 * <tt>next</tt> or <tt>previous</tt>.158 */159 void set(E o);160 161 /**162 * Inserts the specified element into the list (optional operation). The163 * element is inserted immediately before the next element that would be164 * returned by <tt>next</tt>, if any, and after the next element that165 * would be returned by <tt>previous</tt>, if any. (If the list contains166 * no elements, the new element becomes the sole element on the list.)167 * The new element is inserted before the implicit cursor: a subsequent168 * call to <tt>next</tt> would be unaffected, and a subsequent call to169 * <tt>previous</tt> would return the new element. (This call increases170 * by one the value that would be returned by a call to <tt>nextIndex</tt>171 * or <tt>previousIndex</tt>.)172 *173 * @param o the element to insert.174 * @exception UnsupportedOperationException if the <tt>add</tt> method is175 * not supported by this list iterator.176 * 177 * @exception ClassCastException if the class of the specified element178 * prevents it from being added to this list.179 * 180 * @exception IllegalArgumentException if some aspect of this element181 * prevents it from being added to this list.182 */183 void add(E o);184 }185