java中关于Iterator接口和LIstIterator接口的具体介绍
这篇文章主要介绍了java Iterator接口和LIstIterator接口分析的相关资料,需要的朋友可以参考下
java Iterator接口和LIstIterator接口分析
目录
1.Iterator接口
2.ListIterator
3.Iterator和ListIterator的区别
正文
在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的。
我们知道,接口只是一种规范,当继承接口并实现其中的方法时,要遵循接口对方法的说明。
1.Iterator接口
Iterator接口取代了Java集合框架中的Enumeratrion。Iterators不同于enumerations的地方主要有两点:
Iterators允许调用者在迭代过程中从集合里移除元素;
方法名得到了改善。
Iterator源码如下:
/** * An iterator over a collection. {@code Iterator} takes the place of * {@link Enumeration} in the Java Collections Framework. Iterators * differ from enumerations in two ways: * Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. * Method names have been improved. * This interface is a member of the Java Collections Framework. * @param <E> the type of elements returned by this iterator*/ public interface Iterator<E> { /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * @return {@code true} if the iteration has more elements */ boolean hasNext(); /** * Returns the next element in the iteration. * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ E next(); /** * Removes from the underlying collection the last element returned * by this iterator (optional operation). This method can be called * only once per call to {@link #next}. The behavior of an iterator * is unspecified if the underlying collection is modified while the * iteration is in progress in any way other than by calling this * method. * * @implSpec * The default implementation throws an instance of * {@link UnsupportedOperationException} and performs no other action. * * @throws UnsupportedOperationException if the {@code remove} * operation is not supported by this iterator * * @throws IllegalStateException if the {@code next} method has not * yet been called, or the {@code remove} method has already * been called after the last call to the {@code next} * method */ default void remove() { throw new UnsupportedOperationException("remove"); } /** * Performs the given action for each remaining element until all elements * have been processed or the action throws an exception. Actions are * performed in the order of iteration, if that order is specified. * Exceptions thrown by the action are relayed to the caller. * * @implSpec * <p>The default implementation behaves as if: * <pre class="brush:php;toolbar:false">{@code * while (hasNext()) * action.accept(next()); * }* * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEachRemaining(Consumer super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
Iterator接口定义了四个方法以及各个方法的功能,如果有类实现了这个接口,且实现了这些方法,这方法需要实现定义的功能,遵循这些规则:
1).hasNext() 判断容器是否有下一个元素,有则返回true;
2).next() 返回容器中的下一个元素;
3).remove() 移除当前迭代器返回的最后一个元素。这个方法在每次调用next()方法之后只能调用一次;
4).Java 8 增加forEachRemaining方法,它可以实现对余下的所有元素执行指定的操作。
更详细的说明请阅读源码中的注释。
2.ListIterator
ListIterator在Iterator基础上提供了add、set、previous等对列表的操作。但是ListIterator跟Iterator一样,仍是在原列表上进行操作。
ListIterator源码如下:
/** * 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 {@code ListIterator} * has no current element; its <I>cursor position</I> always * lies between the element that would be returned by a call * to {@code previous()} and the element that would be * returned by a call to {@code next()}. * An iterator for a list of length {@code n} has {@code n+1} possible * cursor positions, as illustrated by the carets ({@code ^}) below: * <PRE> * Element(0) Element(1) Element(2) ... Element(n-1) * cursor positions: ^ ^ ^ ^ ^ ** Note that the {@link #remove} and {@link #set(Object)} methods are * not 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()}. * * This interface is a member of the Java Collections Framework.*/ public interface ListIterator
ListIterator的功能更加强大,定义的方法有:
1).hasNext() 向前遍历时,如果有下一个元素返回真;
2).next() 返回下一个元素的值,并将指针加1;
3).hasPrevious() 向相反方向遍历时,如果还有元素返回真;
4).previous() 返回上一个元素的值,并将指针前移1;
5).nextIndex() 返回此时调用next()方法时返回的元素的索引;
6).previousIndex() 返回此时调用previous()方法时返回的元素的索引;
7).remove() 移除最近一次调用next()或previous()方法返回的元素(可选);
8).set(E e) 用元素e将如果此时调用next()或previous()方法返回的元素替换掉;
9).add(E e) 添加元素到此时调用next()返回的元素之前,或此时调用previous()返回的元素之后。
更详细的说明请阅读源码中的注释。
3.Iterator和ListIterator的区别
Iterator和ListIterator的方法对比如下表:
Iterator |
ListIterator |
|
hasNext() |
hasNext() | 覆盖 |
next() |
next() | 覆盖 |
remove() |
remove() | 覆盖 |
forEachRemaining(Consumer super E> action) |
forEachRemaining(Consumer super E> action) | 继承 |
hasPrevious() | ||
previous() | ||
nextIndex() | ||
previousIndex() | ||
set(E e) | ||
add(E e) |
二者的不同之处主要有:
1).Iterator只能单向移动,ListIterator可以双向移动;
2).ListIterator可以删除、替换或添加元素,而Iterator只能删除元素;
3).ListIterator可以返回当前(调用next()或previous()返回的)元素的索引,而Iterator不能。
以上是java中关于Iterator接口和LIstIterator接口的具体介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置
