001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * An iterator for lists that allows the programmer
030 * to traverse the list in either direction, modify
031 * the list during iteration, and obtain the iterator's
032 * current position in the list. A {@code ListIterator}
033 * has no current element; its <I>cursor position</I> always
034 * lies between the element that would be returned by a call
035 * to {@code previous()} and the element that would be
036 * returned by a call to {@code next()}.
037 * An iterator for a list of length {@code n} has {@code n+1} possible
038 * cursor positions, as illustrated by the carets ({@code ^}) below:
039 * <PRE>
040 * Element(0) Element(1) Element(2) ... Element(n-1)
041 * cursor positions: ^ ^ ^ ^ ^
042 * </PRE>
043 * Note that the {@link #remove} and {@link #set(Object)} methods are
044 * <i>not</i> defined in terms of the cursor position; they are defined to
045 * operate on the last element returned by a call to {@link #next} or
046 * {@link #previous()}.
047 *
048 * <p>This interface is a member of the
049 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
050 * Java Collections Framework</a>.
051 *
052 * @author Josh Bloch
053 * @version 1.35, 05/05/07
054 * @see Collection
055 * @see List
056 * @see Iterator
057 * @see Enumeration
058 * @see List#listIterator()
059 * @since 1.2
060 */
061 public interface ListIterator<E> extends Iterator<E> {
062 // Query Operations
063
064 /**
065 * Returns {@code true} if this list iterator has more elements when
066 * traversing the list in the forward direction. (In other words,
067 * returns {@code true} if {@link #next} would return an element rather
068 * than throwing an exception.)
069 *
070 * @return {@code true} if the list iterator has more elements when
071 * traversing the list in the forward direction
072 */
073 boolean hasNext();
074
075 /**
076 * Returns the next element in the list and advances the cursor position.
077 * This method may be called repeatedly to iterate through the list,
078 * or intermixed with calls to {@link #previous} to go back and forth.
079 * (Note that alternating calls to {@code next} and {@code previous}
080 * will return the same element repeatedly.)
081 *
082 * @return the next element in the list
083 * @throws NoSuchElementException if the iteration has no next element
084 */
085 E next();
086
087 /**
088 * Returns {@code true} if this list iterator has more elements when
089 * traversing the list in the reverse direction. (In other words,
090 * returns {@code true} if {@link #previous} would return an element
091 * rather than throwing an exception.)
092 *
093 * @return {@code true} if the list iterator has more elements when
094 * traversing the list in the reverse direction
095 */
096 boolean hasPrevious();
097
098 /**
099 * Returns the previous element in the list and moves the cursor
100 * position backwards. This method may be called repeatedly to
101 * iterate through the list backwards, or intermixed with calls to
102 * {@link #next} to go back and forth. (Note that alternating calls
103 * to {@code next} and {@code previous} will return the same
104 * element repeatedly.)
105 *
106 * @return the previous element in the list
107 * @throws NoSuchElementException if the iteration has no previous
108 * element
109 */
110 E previous();
111
112 /**
113 * Returns the index of the element that would be returned by a
114 * subsequent call to {@link #next}. (Returns list size if the list
115 * iterator is at the end of the list.)
116 *
117 * @return the index of the element that would be returned by a
118 * subsequent call to {@code next}, or list size if the list
119 * iterator is at the end of the list
120 */
121 int nextIndex();
122
123 /**
124 * Returns the index of the element that would be returned by a
125 * subsequent call to {@link #previous}. (Returns -1 if the list
126 * iterator is at the beginning of the list.)
127 *
128 * @return the index of the element that would be returned by a
129 * subsequent call to {@code previous}, or -1 if the list
130 * iterator is at the beginning of the list
131 */
132 int previousIndex();
133
134 // Modification Operations
135
136 /**
137 * Removes from the list the last element that was returned by {@link
138 * #next} or {@link #previous} (optional operation). This call can
139 * only be made once per call to {@code next} or {@code previous}.
140 * It can be made only if {@link #add} has not been
141 * called after the last call to {@code next} or {@code previous}.
142 *
143 * @throws UnsupportedOperationException if the {@code remove}
144 * operation is not supported by this list iterator
145 * @throws IllegalStateException if neither {@code next} nor
146 * {@code previous} have been called, or {@code remove} or
147 * {@code add} have been called after the last call to
148 * {@code next} or {@code previous}
149 */
150 void remove();
151
152 /**
153 * Replaces the last element returned by {@link #next} or
154 * {@link #previous} with the specified element (optional operation).
155 * This call can be made only if neither {@link #remove} nor {@link
156 * #add} have been called after the last call to {@code next} or
157 * {@code previous}.
158 *
159 * @param e the element with which to replace the last element returned by
160 * {@code next} or {@code previous}
161 * @throws UnsupportedOperationException if the {@code set} operation
162 * is not supported by this list iterator
163 * @throws ClassCastException if the class of the specified element
164 * prevents it from being added to this list
165 * @throws IllegalArgumentException if some aspect of the specified
166 * element prevents it from being added to this list
167 * @throws IllegalStateException if neither {@code next} nor
168 * {@code previous} have been called, or {@code remove} or
169 * {@code add} have been called after the last call to
170 * {@code next} or {@code previous}
171 */
172 void set(E e);
173
174 /**
175 * Inserts the specified element into the list (optional operation).
176 * The element is inserted immediately before the next element that
177 * would be returned by {@link #next}, if any, and after the next
178 * element that would be returned by {@link #previous}, if any. (If the
179 * list contains no elements, the new element becomes the sole element
180 * on the list.) The new element is inserted before the implicit
181 * cursor: a subsequent call to {@code next} would be unaffected, and a
182 * subsequent call to {@code previous} would return the new element.
183 * (This call increases by one the value that would be returned by a
184 * call to {@code nextIndex} or {@code previousIndex}.)
185 *
186 * @param e the element to insert
187 * @throws UnsupportedOperationException if the {@code add} method is
188 * not supported by this list iterator
189 * @throws ClassCastException if the class of the specified element
190 * prevents it from being added to this list
191 * @throws IllegalArgumentException if some aspect of this element
192 * prevents it from being added to this list
193 */
194 void add(E e);
195 }
|