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源码
Java 集合 LinkedList的ListIterator的更多相关文章
- Java集合---LinkedList源码解析
一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据remove()7.数据获取get()8.数据复制clo ...
- 6.Java集合-LinkedList实现原理及源码分析
Java中LinkedList的部分源码(本文针对1.7的源码) LinkedList的基本结构 jdk1.7之后,node节点取代了 entry ,带来的变化是,将1.6中的环形结构优化为了直线型链 ...
- java集合LinkedList
基于jdk_1.8.0 关于List,主要是有序的可重复的数据结构.jdk主要实现类有ArrayList(底层使用数组).LinkedList(底层使用双向链表) LinkedList: (一)继承关 ...
- Java集合-LinkedList源码分析
目录 1.数据结构-链表 2.ArrayList结构特性 3.构造方法 4.成员变量 5.常用的成员方法 6.Node节点 7.序列化原理 8.迭代器 9.总结 1.数据结构-链表 链表(Linked ...
- Java集合 LinkedList的原理及使用
Java集合 LinkedList的原理及使用 LinkedList和ArrayList一样是集合List的实现类,虽然较之ArrayList,其使用场景并不多,但同样有用到的时候,那么接下来,我们来 ...
- java集合-LinkedList
一.概述 LinkedList 与 ArrayList 一样实现 List 接口,只是 ArrayList 是 List 接口的大小可变数组的实现,LinkedList 是 List 接口链表的实现. ...
- Java 集合 - LinkedList
一.源码解析 (1). 属性 // 链表长度 transient int size = 0; // 链首和链尾 transient Node<E> first; transient Nod ...
- java:集合输出Iterator,ListIterator,foreach,Enumeration
//集合输出,集合的四种输出 Iterator, ListIterator, foreach, Enumeration 只要碰到集合,第一输出选择是Iterator类. Iterator<E&g ...
- Java集合——LinkedList源码详解
)LinkedList直接继承于AbstractSequentialList,同时实现了List接口,也实现了Deque接口. AbstractSequentialList为顺序访问的数据存储结构提供 ...
随机推荐
- GTK+2.0学习——code::block使用
在之后使用中会慢慢去完善~~ 一.编码设置 1.设置文件编码:setting->editor->如图 2.设置编译时的编码(记住二者要统一):setting->compiler-&g ...
- ASP.NET MVC Url中带点号出现404错误的解决方案
由于项目需求,项目的路由设计如下 config.Routes.MapHttpRoute( name: "Get/Put Sku", routeTemplate: "api ...
- 2.开启TFTP,NFS,SAMBA,SSH服务
一.TFTP (1)dpkg -s tftp-hpa查看服务器端是否安装 (2)如果没安装 sudo apt-get install tftpd-hpa sudo apt-get install tf ...
- find中的-print0和xargs中-0的区别
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...
- 关于float和position
在div 块级元素中,一般我们的div块都是流式的,如果你设定一个div,接下来的div就会另起行,也就是块级元素的定义 但是一般排版不是这样的,最典型的应该就是这种布局了,那么中间的那三个div ...
- The Clocks
The Clocks 题目链接:http://poj.org/problem?id=1166 题意:给出9个时钟的初始状态,问最少通过几次操作,能使每个时钟指向12点(每次操作都会使对应时钟顺时针旋转 ...
- Java中的Class类
Class 类是在Java语言中定义一个特定类的实现.一个类的定义包含成员变量,成员方法,还有这个类实现的接口,以及这个类的父类.Class类的对象用于表示当前运行的 Java 应用程序中的类和接口. ...
- hdu 3507 Print Article(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- Gulp安装使用教程
题记:为什么要使用gulp,网上有很多关于gulp的优势,而在我看来,这些都是工具的优势!工具的优势最主要体现在易用性上,听说gulp比grunt更易用,所以这里写个文档记录. 同样要保证nodejs ...
- datagrid、easyui-dialog
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...