Java 集合 LinkedList的ListIterator

时间:2023-03-09 00:07:18
Java 集合 LinkedList的ListIterator

Java 集合 LinkedList的ListIterator

@author ixenos

摘要:ListIterator<E>是继承自Iterator<E>的接口、listIterator(int index)源码分析、利用ListItr实现的降序迭代

ListIterator<E>是继承自Iterator<E>的接口


  故,ListIterator注意点:

  1.迭代器不存储所有元素的引用,只有两个指针,一个指向上一个返回得到的元素,另一个下一个未涉足的元素;

  2.迭代开始前先同步内外modCount,迭代过程中检查是否同步,如果外部结构改变,则迭代快速失败(fast-fails机制);

  3.ListIterator可以算半个LinkedList视图了,因为在迭代的途中还可以修改外部结构(add、remove)

listIterator(int index)源码分析


listIterator

public ListIterator<E> listIterator(int index)
返回此列表中的元素的列表迭代器(按适当顺序),从列表中指定位置开始
参数:
index - 要从列表迭代器返回的第一个元素的索引(通过调用 next 方法)
返回:
此列表中的元素的 ListIterator(按适当顺序),从列表中指定位置开始
抛出:
IndexOutOfBoundsException - 如果索引超出范围 (index < 0 || index > size())
通过:
   内部类private class ListItr implements ListIterator<E> 实现
 //迭代器不存储数值!是每迭代一次返回一次
// LinkedList中功能强大的ListIterator方法 public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index); //调用内部类ListItr的匿名对象
} //把ListIterator接口送给内部类实现是为了与Iterator接口兼容,因为ListIterator接口继承自Iterator接口
private class ListItr implements ListIterator<E> { //实现了ListIterator接口
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() {
checkForComodification(); //先确定外部modCount没变
if (lastReturned == null)
throw new IllegalStateException(); Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++; //删除外部元素modCount++所以内部的expectedModCount也++来同步
} public void set(E e) {
if (lastReturned == null) //先确定外部modCount没变
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
} public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e); //如果next指针在队尾则直接加在队尾
else
linkBefore(e, next); //否则插入到next指针指向元素的前面
nextIndex++;
expectedModCount++; //删除外部元素modCount++所以内部的expectedModCount也++来同步 } public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action); //如果action为空则抛出空指针异常
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
} //外部结构修改则迭代快速失败fast-fails
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

listIterator源码

利用ListItr实现的降序迭代


descendingIterator

public Iterator<E> descendingIterator()
返回以逆向顺序在此双端队列的元素上进行迭代的迭代器。元素将按从最后一个(尾部)到第一个(头部)的顺序返回。
实质就是在关键操作修改了指针
返回:
以逆向顺序在此双端队列中的元素上进行迭代的迭代器
从以下版本开始:
1.6 
   /**降序迭代
* @since 1.6
*/
// sort 是顺序ascending 表示升descending 表示降
public Iterator<E> descendingIterator() {
return new DescendingIterator();
} /**
* Adapter to provide descending iterators via ListItr.previous
*/
//利用LisItr实现降序迭代
private class DescendingIterator implements Iterator<E> {
private final ListItr itr = new ListItr(size());
public boolean hasNext() {
return itr.hasPrevious(); //利用原有方法,改造了一下return
}
public E next() {
return itr.previous(); //利用原有方法,改造了一下return
}
public void remove() {
itr.remove();
}
}

descendingIterator源码