static void testListIteratorAdd(){
LinkedList<String> strList = new LinkedList<String>();
strList.add("1");
strList.add("2");
strList.add("3");
print("init content:");
printCollection(strList); ListIterator<String> it = strList.listIterator();
it.next();
it.add("1.1");
it.add("1.2"); print("after insert 2 item");
printCollection(strList);
}

输出如下,基本和预期一致,可以连续add,每次add的时候就相当于在光标后面插入,此时可以把迭代器想象为光标。

init content:
collection content:
item:1
item:2
item:3
after insert 2 item
collection content:
item:1
item:1.1
item:1.2
item:2
item:3

关于ListIterator.remove的测试

static void testListIteratorRemove(){
LinkedList<String> strList = new LinkedList<String>();
strList.add("1");
strList.add("2");
strList.add("3");
print("init content:");
printCollection(strList); ListIterator<String> it = strList.listIterator();
it.next();
it.remove();//ok print("after remove 1 item");
printCollection(strList); it.remove();//error
print("after remove 2 item");
printCollection(strList);
}

输出如下,也就是说,ListIterator.remove是依赖于迭代器的状态的,每次调用remove之前,必须先调用一次next或者previous函数。

init content:
collection content:
item:1
item:2
item:3
after remove 1 item
collection content:
item:2
item:3
Exception in thread "main" java.lang.IllegalStateException
at java.util.LinkedList$ListItr.remove(LinkedList.java:923)
at me.ygc.javabasic.learnJava.LearnCollection.testListIteratorRemove(LearnCollection.java:33)
at me.ygc.javabasic.learnJava.LearnCollection.main(LearnCollection.java:15)

Java系列: 关于LinkedList的 ListIterator的add和remove的更多相关文章

  1. 为什么Java里的Arrays.asList不能用add和remove方法?

    在平时的开发过程中,我们知道能够将一个Array的对象转化为List.这种操作,我们仅仅要採用Arrays.asList这种方法即可了.笔者前段时间一直用这种方法,有一天,我发现通过Arrays.as ...

  2. Java系列,《Java核心技术 卷1》,chapter 13,集合

    13.1.2 Java类库中的集合接口和迭代器接口     删除元素,对于next和remove的调用是互相依赖的,如果调用remove之前没有调用next,则会跑出IllegalStateExcep ...

  3. Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. Java 集合 LinkedList的ListIterator

    Java 集合 LinkedList的ListIterator @author ixenos 摘要:ListIterator<E>是继承自Iterator<E>的接口.list ...

  5. Java 集合系列(四)—— ListIterator 源码分析

    以脑图的形式来展示Java集合知识,让零碎知识点形成体系 Iterator 对比   Iterator(迭代器)是一种设计模式,是一个对象,用于遍历集合中的所有元素.  Iterator 包含四个方法 ...

  6. java集合系列之LinkedList源码分析

    java集合系列之LinkedList源码分析 LinkedList数据结构简介 LinkedList底层是通过双端双向链表实现的,其基本数据结构如下,每一个节点类为Node对象,每个Node节点包含 ...

  7. Java小白集合源码的学习系列:LinkedList

    目录 LinkedList 源码学习 LinkedList继承体系 LinkedList核心源码 Deque相关操作 总结 LinkedList 源码学习 前文传送门:Java小白集合源码的学习系列: ...

  8. 【java集合系列】--- LinkedList

    开篇前言--LinkedList中的基本用法 在前面的博文中,小编介绍List接口中的ArrayList集合,List这个接口,有两个实现类,一个就是ArrayList另一个是LinkedList(链 ...

  9. Java容器解析系列(3) List AbstractList ListIterator RandomAccess fail-fast机制 详解

    做为数据结构学习的常规,肯定是先学习线性表,也就是Java中的List,开始 Java中List相关的类关系图如下: 此篇作为对Java中相关类的开篇.从上图中可以看出,List和AbstractLi ...

随机推荐

  1. centos 安装完Nginx后,为什么访问不了?

    很多时候,安装完成后,服务也启动了 但是访问不了 看看是不是防火墙开启了: 本地试下端口是否可访问telnet 192.168.4.155 80 CentOS 7默认使用的是firewall作为防火墙 ...

  2. (ios7) 解决Ios7中,Navigatebar 显示在主View中,和ios6 不一致问题

    在ios 7 系统中 NavigateBar 显示在主View中 ,Ios6 不在主View中,导致后台代码的方式编写View不一致 解决方法: 在ViewControl的viewDidLoad 方法 ...

  3. java打字游戏

    小记:老早之前写的程序,今天发现之前在 csdn上写的东西的图片不显示了,搞得人好郁闷,所以把之前零星的几篇文章搬个家 游戏运行截图: 字母实体类 package com.git.game; impo ...

  4. jquery.qrcode.js 插件生成二维码

    下载地址:https://github.com/jeromeetienne/jquery-qrcode 例子: <!doctype html> <html> <head& ...

  5. ehcache入门

    一.简介 ehcache是一个开源的,纯java进程内的缓存框架.它具有快速,简单,具有多种缓存策略等特点. Hibernate中默认就是用了ehcache.在我们的应用中使用ehcache可以快速地 ...

  6. oracle树操作(select start with connect by prior)

    oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句 ...

  7. jQuery Validate 表单验证插件----自定义校验结果样式

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  8. 烂泥:ubuntu安装vmtools

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于工作需要,需要使用桌面版的linux系统,所以就选择了ubuntu.同时为了方便使用,就在VM中安装ubuntu. 但是为了文件以及操作的方便就 ...

  9. powerdesigner设置表主键列为自动增长

    powerdesigner 版本12.5 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties ———>columens ,双击 ...

  10. Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)

    1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...