博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集合:ListIterator
阅读量:7099 次
发布时间:2019-06-28

本文共 1685 字,大约阅读时间需要 5 分钟。

why ? when ? how ? what ?

Java 集合框架图

7uSztKe.jpg

有了 Iterator 为什么还要有 ListIterator 呢?

Iterator 遍历的时候如果你想修改集合中的元素怎么办? ListIterator来了。

ListIterator有以下功能:

  1. 可以向两个方向(前、后)遍历 List
  2. 在遍历过程中可以修改 list的元素
  3. 将制定的元素插入列表中

UNow1yR.png

ListIterator 有两种获取方式

  1. List.listIterator()
  2. List.listIterator(int location) //可以指定游标的所在位置

内部实现类源码如下:

private class ListItr extends Itr implements ListIterator
{ 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); expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; AbstractList.this.add(i, e); lastRet = -1; cursor = i + 1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }}

参考

转载于:https://www.cnblogs.com/rookieJW/p/9407047.html

你可能感兴趣的文章
DRM-内容数据版权加密保护技术学习(上):视频文件打包实现(转)
查看>>
Html.ActionLink 几种重载方式说明及例子
查看>>
[转]spinlock 理解
查看>>
今天看腾讯在北航的演讲《1亿在线背后的技术挑战》想到的关于MD5算法。
查看>>
SQL高级---SQL Alias(别名)
查看>>
Android开发历程_5(Activity相对布局)
查看>>
OSI
查看>>
方法论、方法论——程序员的阿喀琉斯之踵 (转载)
查看>>
对架构师而言,什么最重要?
查看>>
ecshop foreach控制文章显示条数
查看>>
WPF Button添加图片
查看>>
TabHost的两种实现形式
查看>>
Sharepoint学习笔记—Site Definition系列-- 2、创建Content Type
查看>>
半DDD架构 1
查看>>
数据库 收缩
查看>>
uva 10913 Walking on a Grid
查看>>
Swing中如何让一个TextField获得焦点
查看>>
最近常常干出一些骑着驴找驴的事来
查看>>
The Glowing Python: K- means clustering with scipy
查看>>
配置ORACLE 客户端连接到数据库
查看>>