java容器 接口ListIterator源码分析

本文详细介绍了ListIterator接口,它允许程序员以双向方式遍历列表并进行修改。文章解释了ListIterator的工作原理,包括其游标位置的概念,以及如何通过next和previous方法遍历列表。同时,也介绍了与查询和修改操作相关的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

简介

查询操作(与next,prev相关的6个方法)

修改操作(add,set,remove)


简介

/**
 * 一个对于列表的迭代器,允许编程者无论以哪个方向遍历列表,在迭代中修改列表,获取迭代器在列表中的实时位置。
 * 一个ListIterator总是处于一个由 previous()返回的元素和next()返回的元素中间。
 * 一个对于长度为n的列表,迭代器有n+1个可能的游标位置,以下面的^符号说明:
 * 
 * <PRE>
 *                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
 * cursor positions:  ^            ^            ^            ^                  ^
 * </PRE>
 * 注意:remove和set方法没有按照游标位置定义,它们以最后一次调用next或者previous返回的元素定义。
 *
 *
 * @author  Josh Bloch
 * @see Collection
 * @see List
 * @see Iterator
 * @see Enumeration
 * @see List#listIterator()
 * @since   1.2
 */
public interface ListIterator<E> extends Iterator<E>

查询操作(与next,prev相关的6个方法)

    // Query Operations

    /**
     * 如果列表迭代器当以前进的方向遍历列表时,有更多的元素,返回true。
     * (换言而之,如果next方法返回一个元素,而不是抛出异常,返回true)
     *
     * @return {@code true} if the list iterator has more elements when
     *         traversing the list in the forward direction
     */
    boolean hasNext();

    /**
     * 返回列表中的下一个元素,并前进游标位置。
     * 这个方法可以被重复调用,来迭代列表,或者中间夹杂着调用previous来后退和前进。
     * (注意:交替调用next和previous会重复地返回相同的结果。
     *
     * @return the next element in the list
     * @throws NoSuchElementException if the iteration has no next element
     */
    E next();

    /**
     * 如果列表迭代器当以后退的方向遍历列表时,有更多的元素,返回true。
     * (换言而之,如果previous方法返回一个元素,而不是抛出异常,返回true)
     *
     * @return {@code true} if the list iterator has more elements when
     *         traversing the list in the reverse direction
     */
    boolean hasPrevious();

    /**
     * 返回列表中的上一个元素,并后退游标位置。
     * 这个方法可以被重复调用,来向后迭代列表,或者中间夹杂着调用next来后退和前进。
     * (注意:交替调用next和previous会重复地返回相同的结果。
     *
     * @return the previous element in the list
     * @throws NoSuchElementException if the iteration has no previous
     *         element
     */
    E previous();

    /**
     * 返回接下来调用next返回的元素的index。(如果迭代器处于列表末尾,返回列表大小)
     *
     * @return the index of the element that would be returned by a
     *         subsequent call to {@code next}, or list size if the list
     *         iterator is at the end of the list
     */
    int nextIndex();

    /**
     * 返回接下来调用previous返回的元素的index。(如果迭代器处于列表开始,返回-1)
     *
     * @return the index of the element that would be returned by a
     *         subsequent call to {@code previous}, or -1 if the list
     *         iterator is at the beginning of the list
     */
    int previousIndex();

修改操作(add,set,remove)

    // Modification Operations

    /**
     * 移除由next或previous返回的最后元素。
     * 对于一次next或previous,仅仅调用一次这个方法。
     * 当且仅当next或previous调用后,没有调用add方法,才能使用remove方法。
     *
     * @throws UnsupportedOperationException if the {@code remove}
     *         operation is not supported by this list iterator
     * @throws IllegalStateException if neither {@code next} nor
     *         {@code previous} have been called, or {@code remove} or
     *         {@code add} have been called after the last call to
     *         {@code next} or {@code previous}
     */
    void remove();

    /**
     * 将由next或previous返回的最后元素替换为指定元素(可选操作)。
     * 当且仅当next或previous调用后,没有调用remove和add方法,才能使用set方法。
     *
     * @param e the element with which to replace the last element returned by
     *          {@code next} or {@code previous}
     * @throws UnsupportedOperationException if the {@code set} operation
     *         is not supported by this list iterator
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws IllegalArgumentException if some aspect of the specified
     *         element prevents it from being added to this list
     * @throws IllegalStateException if neither {@code next} nor
     *         {@code previous} have been called, or {@code remove} or
     *         {@code add} have been called after the last call to
     *         {@code next} or {@code previous}
     */
    void set(E e);

    /**
     * 插入指定元素到列表(可选操作)
     * 元素被迅速地插入,在next返回的元素之前,在previous返回的元素之后,
     * (如果列表没有元素,新的元素成为列表唯一一个元素)
     * 新的元素被插入到隐藏的游标前,接下来调用next不会被影响,接下来调用previous回访一个新的元素
     * (这个调用增加了nextIndex或previousIndex返回的值)
     *
     * @param e the element to insert
     * @throws UnsupportedOperationException if the {@code add} method is
     *         not supported by this list iterator
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws IllegalArgumentException if some aspect of this element
     *         prevents it from being added to this list
     */
    void add(E e);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值