迭代器
作者:互联网
之前对迭代器的理解一直存在误区
ListIterator
首先迭代器都是实现了Iterator()接口的,这里面定义了一些方法规范,我们的集合框架都要实现Iterable接口,他里面定义了一个返回迭代器的方法,也就是说,所有的集合框架都要有迭代器让外界拿到用来对集合进行遍历
迭代器没有当前元素一说,他只是一个游标(cursor),这个游标总在元素之间
每次调用next()方法游标向后移动一位,跨过一个元素,并返回跨过的元素
对于ListIterator来说,成员变量next就理解为游标
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
....
}
当调用next()的时候,游标会向后移动一位,跨过一个元素,而这个跨过的元素会作为返回值返回
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
调用previous()会向前移动一位,返回的还是刚刚越过的元素,所以我先调用next(),越过一个元素,在调用previous()返回到刚刚位置,迭代器拿到的都是同一个(他们越过的元素)
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
而对于所以的remove()方法来说,都是要先调用next()方法(previous()),再调用remove()方法,因为remove()方法是对上一个返回的元素进行操作,而再调用remove()后会把lastReturned置为null
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
ListIterator
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public boolean hasPrevious() {
return nextIndex > 0;
}
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {
//remove操作的都是lastReturned上一个返回的
checkForComodification();
//必须先迭代才能remove
if(lastReturned == null){
throw new IllegalStateException();
}
//接下来又要分两种情况
//①向前迭代后删除 这个时候lastReturned == next,我们需要把next重新赋值
//②向后迭代后
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if(lastReturned == next){
next = lastNext;
}else {
nextIndex--;
}
lastReturned = null;
expectedModCount++;
}
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
今天又学java了
发布了50 篇原创文章 · 获赞 0 · 访问量 687
私信
关注
标签:Node,lastReturned,迭代,private,next,nextIndex,public 来源: https://blog.csdn.net/weixin_43907800/article/details/104056017