Iterator接口
这个接口下的方法:
一个简单的例子先让你了解迭代器模式:
https://download.csdn.net/download/weixin_43860530/12347734
Iterator 是个接口,定义了几个方法:
(迭代位置也称游标,它本身不知向任何元素,我们可以把它理解为两个元素之间的位置)
- hasNext(),返回是否有下一个元素。
- next(),获取下一个元素,迭代位置往后移一位。
- remove(),删除最后一次next()返回的元素,比如我们执行了next()之后,在执行remove(),就会删除容器中的刚刚执行next()时返回的元素。
- forEachRemaining(Consumer<? super E> action) 遍历剩余元素,并执行给定操作
Iterable接口
- iterator(),返回迭代器
- spliterator(),返回spliterator
- forEach(Consumer<? super T>)遍历每一个容器内元素
Iterable和Iterator的区别和联系:
我们发现Collection是实现Iterable的,不仅仅是它,很多集合类都是实现Iterable,那为什么不直接实现Iterator呢,要绕个弯呢?有一个重要的原因就是:如果直接实现Iterator接口,那必须在容器内重写next()和hasNext()方法,而这两个方法肯定需要一个变量用来保存当前迭代位置,所以这个变量肯定会在容器内声明,当容器出现添加或者删除等改变操作时,迭代器位置就需要一系列操作使其对应到新的位置,如果是实现Iterable,须在iterator()返回一个自定义的Iterator,每次调用iterator()都可以返回一个从头开始迭代的迭代器。而且多个迭代器之间不相互干扰。
ListIterator
继承了Iterator,对Iterator进行扩展:
- hasPrevious(),判断前面是否有元素
- previous(),返回前面元素,迭代位置往前移一位
- nextIndex(),返回后一个元素索引位置
- previousIndex(),返回前一个元素索引位置
- add(E),在迭代位置前插入一个元素
- set(E),更新最后一次next()返回的元素
看个例子:
AbstractList.java中有个内部类实现了ListIterator
private class ListItr extends AbstractList<E>.Itr implements ListIterator<E> {
ListItr(int var2) {
super(null);
this.cursor = var2;
}
public boolean hasPrevious() {
return this.cursor != 0;
}
public E previous() {
this.checkForComodification();
try {
int var1 = this.cursor - 1;
Object var2 = AbstractList.this.get(var1);
this.lastRet = this.cursor = var1;
return var2;
} catch (IndexOutOfBoundsException var3) {
this.checkForComodification();
throw new NoSuchElementException();
}
}
public int nextIndex() {
return this.cursor;
}
public int previousIndex() {
return this.cursor - 1;
}
public void set(E var1) {
if (this.lastRet < 0) {
throw new IllegalStateException();
} else {
this.checkForComodification();
try {
AbstractList.this.set(this.lastRet, var1);
this.expectedModCount = AbstractList.this.modCount;
} catch (IndexOutOfBoundsException var3) {
throw new ConcurrentModificationException();
}
}
}
public void add(E var1) {
this.checkForComodification();
try {
int var2 = this.cursor;
AbstractList.this.add(var2, var1);
this.lastRet = -1;
this.cursor = var2 + 1;
this.expectedModCount = AbstractList.this.modCount;
} catch (IndexOutOfBoundsException var3) {
throw new ConcurrentModificationException();
}
}
}
AbstractList还有另一个内部类实现了Iterator
private class Itr implements Iterator<E> {
int cursor;
int lastRet;
int expectedModCount;
private Itr() {
this.cursor = 0;
this.lastRet = -1;
this.expectedModCount = AbstractList.this.modCount;
}
public boolean hasNext() {
return this.cursor != AbstractList.this.size();
}
public E next() {
this.checkForComodification();
try {
int var1 = this.cursor;
Object var2 = AbstractList.this.get(var1);
this.lastRet = var1;
this.cursor = var1 + 1;
return var2;
} catch (IndexOutOfBoundsException var3) {
this.checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (this.lastRet < 0) {
throw new IllegalStateException();
} else {
this.checkForComodification();
try {
AbstractList.this.remove(this.lastRet);
if (this.lastRet < this.cursor) {
--this.cursor;
}
this.lastRet = -1;
this.expectedModCount = AbstractList.this.modCount;
} catch (IndexOutOfBoundsException var2) {
throw new ConcurrentModificationException();
}
}
}
final void checkForComodification() {
if (AbstractList.this.modCount != this.expectedModCount) {
throw new ConcurrentModificationException();
}
}
}