Java集合(3)——ListIterator接口源码 & ListIterator接口与Iterator接口辨析
2018-02-08 17:47
567 查看
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 ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to
previous()and the element that would be returned by a call to
next().An iterator for a list of length n has n+1 possible cursor positions, as illustrated by the carets (^) below:
翻译: 允许开发人员遍历任意方向的集合的迭代器,可以在迭代过程中修改列表,也可以获取迭代器当前位置。一个ListIterator没有当前的元素,它的cursor位置总是在两个元素之间。这两个元素可以一个被
previous()方法返回,一个被
next()返回。长度为n的集合有n+1个可能的cursor位置。
ListIterator接口中的成员方法
package java.util; public interface ListIterator<E> extends Iterator<E> { // Query Operations boolean hasNext(); E next(); boolean hasPrevious(); E previous(); int nextIndex(); int previousIndex(); // Modification Operations void remove(); void set(E e); void add(E e); }
ListIterator是一个更加强大的Iterator子接口,它只能用于各种List接口的子类访问。因为ListIterator没有实现类,只有List接口中有一个返回值类型为ListIterator的
listIterator()方法。而AbstractList作为List的直接子类,实现了
listIterator()方法。
在AbstractList中实现Iterator接口使用了一个方法,一个内部类。实现ListIterator接口使用了两个方法,两个内部类。
AbstractList抽象类中实现Iterator接口源码解析
使用了一个Public Iterator<E> iterator()方法和
private class Itr implements Iterator<E>类
Public Iterator<E> iterator()
方法源码
public Iterator<E> iterator() { return new Itr(); }
可以看出在这个方法中只是创建了Itr类的对象,所以主要的实现过程还要看Itr类。
private class Itr implements Iterator<E>
类源码
该类包含3个成员变量,4个需要实现Ietrator接口的方法。(该部分涉及到fail-fast机制,也叫ConcurrentModificationException异常)protected transient int modCount = 0; private class Itr implements Iterator<E> { int cursor = 0; //游标位置,初始设置为0 int lastRet = -1; //当前游标位置的前一个位置,初始设置为-1 int expectedModCount = modCount; //游标中的ModCount值,如果与集合中的ModCount值不同就会抛出异常! public boolean hasNext() { return cursor != size(); } public E next() { checkForComodification(); try { int i = cursor; E next = get(i); lastRet = i; cursor = i + 1; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
AbstractList抽象类中实现ListIterator接口源码解析
实现该方法有两种方式,一种是无参数的,一种是有参数的public ListIterator<E> listIterator() { return listIterator(0); } public ListIterator<E> listIterator(final int index) { rangeCheckForAdd(index); return new ListItr(index); }
从上述源码中可以看出,无参数的
listIterator()方法调用的是有参数的
listIterator(fianl int index)方法,只不过将参数值设置为0。有参数的方法先判断Index值是否符合范围,如果符合范围,就调用内部类
ListItr
private class ListItr extends Itr implements ListIterator<E>
类源码
private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { cursor = index; } public boolean hasPrevious() { return cursor != 0; } public E previous() { //注意:向前操作也是两步,第一步是得到游标的前一个元素,第二步是将游标的当前位置赋值给游标的下一个位置 checkForComodification(); try { int i = cursor - 1; E previous = get(i); lastRet = cursor = i; return previous; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public int nextIndex() { return cursor; } public int previousIndex() { return cursor-1; } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.set(lastRet, e);//这个`set()`方法在抽象类中没有具体实现,方法体只是抛出了异常,具体操作需要子类自己实现 expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; AbstractList.this.add(i, e); //这个`add()`方法在抽象类中没有具体实现,方法体只是抛出了异常,具体操作需要子类自己实现 lastRet = -1; cursor = i + 1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } }
可以看出该类继承了刚刚的内部类Itr,然后实现了ListIterator接口,实现了接口中的方法。
Iterator接口和ListIterator接口的主要区别
ListIterator有add()方法,可以向List中添加对象,而Iterator不能添加对象
Iterator只能向前遍历,但是ListIterator可以双向移动,它还可以产生相对于迭代器在列表中指向的前一个和后一个元素的位置索引。最主要的是ListIterator可以反向迭代List
ListIterator可以使用
set()方法替换他访问过的最后一个元素。但是Iterator只能遍历集合,不能修改集合。
感谢
http://blog.csdn.net/u011240877/article/details/52752589http://blog.csdn.net/a597926661/article/details/7679765
https://docs.oracle.com/javase/8/docs/api/
相关文章推荐
- JAVA基础再回首(十四)——集合框架、Collection接口、迭代器Iterator、List接口、ListIterator列表迭代器
- Java核心类库——集合的迭代(遍历) Iterator接口
- Java集合----概述、Collection接口、Iterator接口
- java 1.8 集合源码解析1:Iterable和Iterator
- Java集合框架学习(十四) Iterator接口详解
- Java集合排序之compareTo&compare 接口 compareable & comparator
- java:集合输出之Iterator和ListIterator二
- JAVA提高篇(31)--Iterator和ListIterator 迭代器的使用(遍历集合)
- Java集合之Stack 源码分析 泽…
- JAVA中ListIterator和Iterator详解与辨析
- JAVA中ListIterator和Iterator详解与辨析
- Java集合源码学习(6)_List接口的实现_ArrayList_Vector
- java 集合框架之LinkedList及ListIterator实现源码分析
- Java API ——Collection集合类 & Iterator接口
- Java集合源码学习(13)_Queue接口以及基础实现AbstractQueue
- Java基础——集合源码解析 List List 接口
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与Lin
- Java中集合框架,Collection接口、Set接口、List接口、Map接口,已经常用的它们的实现类,简单的JDK源码分析底层实现
- java:集合输出Iterator,ListIterator,foreach,Enumeration
- java_集合体系之Collection框架相关抽象类接口详解、源码——02