Java Coder

Iterator和ListIterator接口分析

Iterator接口定义了遍历集合类对象的通用方法,不同的集合类有不同的Iterator实现类,实现接口中的抽象方法。有了Iterator接口,可以用统一的方式对各种集合元素进行遍历和迭代,方便程序的修改。
Iterator接口中包括的方法有:
 1 public interface Iterator<E> {
 2     /**
 3      * Returns <tt>true</tt> if the iteration has more elements. (In other
 4      * words, returns <tt>true</tt> if <tt>next</tt> would return an element
 5      * rather than throwing an exception.)
 6      *
 7      * @return <tt>true</tt> if the iterator has more elements.
 8      */
 9     boolean hasNext();
10 
11     /**
12      * Returns the next element in the iteration.
13      *
14      * @return the next element in the iteration.
15      * @exception NoSuchElementException iteration has no more elements.
16      */
17     E next();
18 
19     /**
20      * 
21      * Removes from the underlying collection the last element returned by the
22      * iterator (optional operation).  This method can be called only once per
23      * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
24      * the underlying collection is modified while the iteration is in
25      * progress in any way other than by calling this method.
26      *
27      * @exception UnsupportedOperationException if the <tt>remove</tt>
28      *          operation is not supported by this Iterator.
29      
30      * @exception IllegalStateException if the <tt>next</tt> method has not
31      *          yet been called, or the <tt>remove</tt> method has already
32      *          been called after the last call to the <tt>next</tt>
33      *          method.
34      */
35     void remove();
36 }

ListIterator接口扩展了Iterator接口,定义了更多的访问方法。不仅可以向前遍历,还可以向后遍历,添加、修改元素。ListIterator接口中包括的方法有:
  1 public interface ListIterator<E> extends Iterator<E> {
  2     // Query Operations
  3 
  4     /**
  5      * Returns <tt>true</tt> if this list iterator has more elements when
  6      * traversing the list in the forward direction. (In other words, returns
  7      * <tt>true</tt> if <tt>next</tt> would return an element rather than
  8      * throwing an exception.)
  9      *
 10      * @return <tt>true</tt> if the list iterator has more elements when
 11      *        traversing the list in the forward direction.
 12      */
 13     boolean hasNext();
 14 
 15     /**
 16      * Returns the next element in the list.  This method may be called
 17      * repeatedly to iterate through the list, or intermixed with calls to
 18      * <tt>previous</tt> to go back and forth.  (Note that alternating calls
 19      * to <tt>next</tt> and <tt>previous</tt> will return the same element
 20      * repeatedly.)
 21      *
 22      * @return the next element in the list.
 23      * @exception NoSuchElementException if the iteration has no next element.
 24      */
 25     E next();
 26 
 27     /**
 28      * Returns <tt>true</tt> if this list iterator has more elements when
 29      * traversing the list in the reverse direction.  (In other words, returns
 30      * <tt>true</tt> if <tt>previous</tt> would return an element rather than
 31      * throwing an exception.)
 32      *
 33      * @return <tt>true</tt> if the list iterator has more elements when
 34      *           traversing the list in the reverse direction.
 35      */
 36     boolean hasPrevious();
 37 
 38     /**
 39      * Returns the previous element in the list.  This method may be called
 40      * repeatedly to iterate through the list backwards, or intermixed with
 41      * calls to <tt>next</tt> to go back and forth.  (Note that alternating
 42      * calls to <tt>next</tt> and <tt>previous</tt> will return the same
 43      * element repeatedly.)
 44      *
 45      * @return the previous element in the list.
 46      *
 47      * @exception NoSuchElementException if the iteration has no previous
 48      *            element.
 49      */
 50     E previous();
 51 
 52     /**
 53      * Returns the index of the element that would be returned by a subsequent
 54      * call to <tt>next</tt>. (Returns list size if the list iterator is at the
 55      * end of the list.)
 56      *
 57      * @return the index of the element that would be returned by a subsequent
 58      *            call to <tt>next</tt>, or list size if list iterator is at end
 59      *           of list.
 60      */
 61     int nextIndex();
 62 
 63     /**
 64      * Returns the index of the element that would be returned by a subsequent
 65      * call to <tt>previous</tt>. (Returns -1 if the list iterator is at the
 66      * beginning of the list.)
 67      *
 68      * @return the index of the element that would be returned by a subsequent
 69      *            call to <tt>previous</tt>, or -1 if list iterator is at
 70      *           beginning of list.
 71      */
 72     int previousIndex();
 73 
 74 
 75     // Modification Operations
 76 
 77     /**
 78      * Removes from the list the last element that was returned by
 79      * <tt>next</tt> or <tt>previous</tt> (optional operation).  This call can
 80      * only be made once per call to <tt>next</tt> or <tt>previous</tt>.  It
 81      * can be made only if <tt>ListIterator.add</tt> has not been called after
 82      * the last call to <tt>next</tt> or <tt>previous</tt>.
 83      *
 84      * @exception UnsupportedOperationException if the <tt>remove</tt>
 85      *            operation is not supported by this list iterator.
 86      * @exception IllegalStateException neither <tt>next</tt> nor
 87      *            <tt>previous</tt> have been called, or <tt>remove</tt> or
 88      *            <tt>add</tt> have been called after the last call to
 89      *            <tt>next</tt> or <tt>previous</tt>.
 90      */
 91     void remove();
 92 
 93     /**
 94      * Replaces the last element returned by <tt>next</tt> or
 95      * <tt>previous</tt> with the specified element (optional operation).
 96      * This call can be made only if neither <tt>ListIterator.remove</tt> nor
 97      * <tt>ListIterator.add</tt> have been called after the last call to
 98      * <tt>next</tt> or <tt>previous</tt>.
 99      *
100      * @param e the element with which to replace the last element returned by
101      *          <tt>next</tt> or <tt>previous</tt>.
102      * @exception UnsupportedOperationException if the <tt>set</tt> operation
103      *           is not supported by this list iterator.
104      * @exception ClassCastException if the class of the specified element
105      *           prevents it from being added to this list.
106      * @exception IllegalArgumentException if some aspect of the specified
107      *          element prevents it from being added to this list.
108      * @exception IllegalStateException if neither <tt>next</tt> nor
109      *              <tt>previous</tt> have been called, or <tt>remove</tt> or
110      *          <tt>add</tt> have been called after the last call to
111      *           <tt>next</tt> or <tt>previous</tt>.
112      */
113     void set(E e);
114 
115     /**
116      * Inserts the specified element into the list (optional operation).  The
117      * element is inserted immediately before the next element that would be
118      * returned by <tt>next</tt>, if any, and after the next element that
119      * would be returned by <tt>previous</tt>, if any.  (If the list contains
120      * no elements, the new element becomes the sole element on the list.)
121      * The new element is inserted before the implicit cursor: a subsequent
122      * call to <tt>next</tt> would be unaffected, and a subsequent call to
123      * <tt>previous</tt> would return the new element.  (This call increases
124      * by one the value that would be returned by a call to <tt>nextIndex</tt>
125      * or <tt>previousIndex</tt>.)
126      *
127      * @param e the element to insert.
128      * @exception UnsupportedOperationException if the <tt>add</tt> method is
129      *           not supported by this list iterator.
130      *
131      * @exception ClassCastException if the class of the specified element
132      *           prevents it from being added to this list.
133      *
134      * @exception IllegalArgumentException if some aspect of this element
135      *            prevents it from being added to this list.
136      */
137     void add(E e);
138 }


posted on 2008-07-19 11:53 fred.li 阅读(581) 评论(0)  编辑  收藏 所属分类: java.util 包分析


只有注册用户登录后才能发表评论。


网站导航: