`

ListIterator接口

阅读更多
/*
 * @(#)ListIterator.java	1.23 03/12/19
 *
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.util;

/**
 * 
 * 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 <TT>ListIterator</TT> 
 * has no current element; its <I>cursor position</I> always 
 * lies between the element that would be returned by a call 
 * to <TT>previous()</TT> and the element that would be 
 * returned by a call to <TT>next()</TT>. In a list of 
 * length <TT>n</TT>, there are <TT>n+1</TT> valid 
 * index values, from <TT>0</TT> to <TT>n</TT>, inclusive. 
 * <PRE>
 *
 *          Element(0)   Element(1)   Element(2)   ... Element(n)   
 *        ^            ^            ^            ^               ^
 * Index: 0            1            2            3               n+1
 *
 * </PRE>
 * <P>
 * Note that the {@link #remove} and {@link #set(Object)} methods are
 * <i>not</i> defined in terms of the cursor position;  they are defined to
 * operate on the last element returned by a call to {@link #next} or {@link
 * #previous()}.
 * <P>
 * This interface is a member of the 
 * <a href="{@docRoot}/../guide/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @author  Josh Bloch
 * @version 1.23, 12/19/03
 * @see Collection
 * @see List
 * @see Iterator
 * @see Enumeration
 * @since   1.2
 */
public interface ListIterator<E> extends Iterator<E> {
    // Query Operations

    /**
     * Returns <tt>true</tt> if this list iterator has more elements when
     * traversing the list in the forward direction. (In other words, returns
     * <tt>true</tt> if <tt>next</tt> would return an element rather than
     * throwing an exception.)
     *
     * @return <tt>true</tt> if the list iterator has more elements when
     *		traversing the list in the forward direction.
     */
    boolean hasNext();

    /**
     * Returns the next element in the list.  This method may be called
     * repeatedly to iterate through the list, or intermixed with calls to
     * <tt>previous</tt> to go back and forth.  (Note that alternating calls
     * to <tt>next</tt> and <tt>previous</tt> will return the same element
     * repeatedly.)
     *
     * @return the next element in the list.
     * @exception NoSuchElementException if the iteration has no next element.
     */
    E next();

    /**
     * Returns <tt>true</tt> if this list iterator has more elements when
     * traversing the list in the reverse direction.  (In other words, returns
     * <tt>true</tt> if <tt>previous</tt> would return an element rather than
     * throwing an exception.)
     *
     * @return <tt>true</tt> if the list iterator has more elements when
     *	       traversing the list in the reverse direction.
     */
    boolean hasPrevious();

    /**
     * Returns the previous element in the list.  This method may be called
     * repeatedly to iterate through the list backwards, or intermixed with
     * calls to <tt>next</tt> to go back and forth.  (Note that alternating
     * calls to <tt>next</tt> and <tt>previous</tt> will return the same
     * element repeatedly.)
     *
     * @return the previous element in the list.
     * 
     * @exception NoSuchElementException if the iteration has no previous
     *            element.
     */
    E previous();

    /**
     * Returns the index of the element that would be returned by a subsequent
     * call to <tt>next</tt>. (Returns list size if the list iterator is at the
     * end of the list.)
     *
     * @return the index of the element that would be returned by a subsequent
     * 	       call to <tt>next</tt>, or list size if list iterator is at end
     *	       of list. 
     */
    int nextIndex();

    /**
     * Returns the index of the element that would be returned by a subsequent
     * call to <tt>previous</tt>. (Returns -1 if the list iterator is at the
     * beginning of the list.)
     *
     * @return the index of the element that would be returned by a subsequent
     * 	       call to <tt>previous</tt>, or -1 if list iterator is at
     *	       beginning of list.
     */ 
    int previousIndex();


    // Modification Operations
    
    /**
     * Removes from the list the last element that was returned by
     * <tt>next</tt> or <tt>previous</tt> (optional operation).  This call can
     * only be made once per call to <tt>next</tt> or <tt>previous</tt>.  It
     * can be made only if <tt>ListIterator.add</tt> has not been called after
     * the last call to <tt>next</tt> or <tt>previous</tt>.
     *
     * @exception UnsupportedOperationException if the <tt>remove</tt>
     * 		  operation is not supported by this list iterator.
     * @exception IllegalStateException neither <tt>next</tt> nor
     *		  <tt>previous</tt> have been called, or <tt>remove</tt> or
     *		  <tt>add</tt> have been called after the last call to *
     *		  <tt>next</tt> or <tt>previous</tt>.
     */
    void remove();

    /**
     * Replaces the last element returned by <tt>next</tt> or
     * <tt>previous</tt> with the specified element (optional operation).
     * This call can be made only if neither <tt>ListIterator.remove</tt> nor
     * <tt>ListIterator.add</tt> have been called after the last call to
     * <tt>next</tt> or <tt>previous</tt>.
     *
     * @param o the element with which to replace the last element returned by
     *          <tt>next</tt> or <tt>previous</tt>.
     * @exception UnsupportedOperationException if the <tt>set</tt> operation
     * 		  is not supported by this list iterator.
     * @exception ClassCastException if the class of the specified element
     * 		  prevents it from being added to this list.
     * @exception IllegalArgumentException if some aspect of the specified
     *		  element prevents it from being added to this list.
     * @exception IllegalStateException if neither <tt>next</tt> nor
     *	          <tt>previous</tt> have been called, or <tt>remove</tt> or
     *		  <tt>add</tt> have been called after the last call to
     * 		  <tt>next</tt> or <tt>previous</tt>.
     */
    void set(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 <tt>next</tt>, if any, and after the next element that
     * would be returned by <tt>previous</tt>, 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 <tt>next</tt> would be unaffected, and a subsequent call to
     * <tt>previous</tt> would return the new element.  (This call increases
     * by one the value that would be returned by a call to <tt>nextIndex</tt>
     * or <tt>previousIndex</tt>.)
     *
     * @param o the element to insert.
     * @exception UnsupportedOperationException if the <tt>add</tt> method is
     * 		  not supported by this list iterator.
     * 
     * @exception ClassCastException if the class of the specified element
     * 		  prevents it from being added to this list.
     * 
     * @exception IllegalArgumentException if some aspect of this element
     *            prevents it from being added to this list.
     */
    void add(E o);
}

 

分享到:
评论

相关推荐

    java Iterator接口和LIstIterator接口分析

    Java Iterator接口和ListIterator接口分析 Java Iterator接口和ListIterator接口是Java集合框架中的两个重要接口,分别用于迭代集合中的元素和列表中的元素。在本文中,我们将详细介绍Java Iterator接口和...

    Iterator接口

    Iterator接口和ListIterator接口的使用可以带来很多便利,例如,使用Iterator接口可以遍历集合中的元素,而不管集合的具体实现类型,同时也可以使用ListIterator接口来控制迭代器的行为,例如,添加或删除元素。...

    java常用集合类总结

    Iterator接口用于访问集合中的元素,而ListIterator接口用于访问List集合中的元素。 Properties类是Java集合类中的一种特殊类,以键值对的形式存储数据,但只能存储字符串对。Properties类提供了两个方法:set...

    ArrayList LinkedList Vector区别

    List 接口提供了一个 listIterator() 方法,返回一个 ListIterator 接口,和标准的 Iterator 接口相比,ListIterator 多了一些 add() 之类的方法,允许添加、删除、设定元素,还能向前或向后遍历。 LinkedList、...

    List,set,Map 的用法和区别

    除了具有 Collection 接口必备的 iterator() 方法外,List 还提供一个 listIterator() 方法,返回一个 ListIterator 接口,和标准的 Iterator 接口相比,ListIterator 多了一些 add() 之类的方法,允许添加、删除、...

    javase知识的回顾

    6. **ListIterator接口**:扩展了Iterator接口,支持双向遍历列表。 7. **Enumeration接口**:较旧的迭代接口,主要用于早期的集合类,如Vector和Hashtable。 8. **SortedSet和SortedMap接口**:提供了排序功能,...

    java学校人事管理系统实验报告1.docx

    例如,在 Personnel 类中,使用了 ArrayList 类来存储人员信息,并使用 ListIterator 接口来遍历人员列表。 知识点 5:Java 语言的异常处理 该实验报告没有涉及到异常处理的内容,但是在实际应用中,异常处理是...

    ArrayList LinkList和vector的区别

    除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素,还能...

    Java-Java集合体系-List-Set

    特有的ListIterator接口提供了双向遍历的能力。ArrayList是List接口的常用实现类,它基于动态扩展的数组实现,提供高效随机访问,但插入和删除元素相对耗时。 二、ArrayList类 ArrayList类是ArrayList接口的主要...

    Java程序员集合框架面试题-java集合框架面试题.docx

    3. **ListIterator接口** `ListIterator`扩展了`Iterator`接口,专门用于遍历`List`类型的集合,支持双向遍历,可以向前和向后移动,并且能够修改集合中的元素,这在处理列表时非常有用。 4. **HashMap与HashTable...

    程序员面试宝典 java.pdf,这是一份不错的文件

    【ListIterator接口】ListIterator是Iterator的子接口,专为List接口设计,除了提供基本的遍历功能外,还支持双向遍历和修改元素,包括添加元素和替换元素。 【HashMap与HashTable】HashMap是Java集合框架中实现Map...

    《Java和Android开发实战详解》第6到10章源代码-by 南邮-陈杨

    第6章 数组与字符串 96 6.1 数组基础 96 6.2 一维数组与多维数组 96 6.2.1 一维数组 97 6.2.2 二维与多维数组 99 6.2.3 可变长度的参数列表 101 6.3 字符串类 102 ...10.6.4 ListIterator接口 213

    Java语言的Util类详细介绍

    除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加、删除、设定元素,还能...

    集合框架以及泛型的相关应用

    5. **ListIterator接口**:`ListIterator`是`Iterator`的子接口,专为`List`设计,增加了向前和向后遍历的能力,还有`add()`、`set()`和`previous()`等方法。 6. **并发修改异常**:在迭代过程中,如果通过集合对象...

    Java集合框架使用总结

    - `ListIterator`接口继承自`Iterator`,主要用于遍历`List`集合。它不仅支持正向和反向遍历,还支持添加、替换、获取当前位置等功能。 #### 四、集合框架之外的 Map 接口 **1. Map 接口简介** - `Map`接口将键...

    Java容器类学习心得.pdf

    - **ListIterator接口**:专用于List的迭代,增加了`previous()`返回前一个元素和`hasPrevious()`检查是否有前一个元素的能力,还有`add(E e)`在当前位置插入元素。 - **SortedMap接口**:扩展了Map接口,保证了键...

    互联网高频Java后端面试题20道(适合1~3年).docx

    此外,还有Iterator和ListIterator接口用于遍历集合,以及Queue和Deque接口用于队列和双端队列操作。 解析:理解Java集合框架能帮助开发者有效地管理数据结构,选择合适的容器以满足性能和功能需求。例如,List接口...

    data structure in Java

    - **ListIterator接口**:除了基本的迭代功能外,还提供了添加、删除和替换元素的功能。 ##### 2.2 Java集合抽象 - **Collection接口**:定义了一个包含多个对象的容器的基本行为。 - **Set接口**:不允许重复元素...

Global site tag (gtag.js) - Google Analytics