ListIterator add remove 使用注意
add方法示例
//在最前面添加List<String> list1 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator1 = list1.listIterator();listIterator1.add("D");listIterator1.add("E");System.out.println(list1);//[D, E, a, b, c]//在最后面添加List<String> list2 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator2 = list2.listIterator();while (listIterator2.hasNext()) {listIterator2.next();}listIterator2.add("D");listIterator2.add("E");System.out.println(list2);//[a, b, c, D, E]//在每个元素的前面和后面都添加List<String> list3 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator3 = list3.listIterator();while (listIterator3.hasNext()) {listIterator3.add("前面");listIterator3.next();listIterator3.add("后面");}System.out.println(list3);//[前面, a, 后面, 前面, b, 后面, 前面, c, 后面]//在指定元素的前面和后面添加List<String> list4 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator4 = list4.listIterator();while (listIterator4.hasNext()) {if (listIterator4.next().equals("a")) {//现在指向的是a的后面listIterator4.previous();//先重新指向a的前面,这里不用担心NoSuchElementExceptionlistIterator4.add("前面");//在前面添加元素,添加后还是指向的a的前面listIterator4.next();//向后【再】移动一位,现在指向的是a的后面listIterator4.add("后面");//在a的后面添加元素}}System.out.println(list4);//[前面, a, 后面, b, c]
remove方法
remove必须要跟在next()或是previous()之后,而且只能执行一次,删多个元素,需要再执行next()或previous()。在执行next()或previous()后不能先执行了 add()方法。因为add()方法执行以后,迭代器已经移动了,这样所要删除的目标元素指向不明,会报异常。//可以直接add,但不能直接remove,remove必须放在next之后try {List<String> list1 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator1 = list1.listIterator();listIterator1.remove();} catch (Exception e) {System.out.println("直接remove会报 IllegalStateException");}//标准的做法:在next之后才能removeList<String> list2 = new LinkedList<String>(Arrays.asList(new String[] { "b", "a", "b", "c", "b", }));ListIterator<String> listIterator2 = list2.listIterator();while (listIterator2.hasNext()) {if (listIterator2.next().equals("b")) listIterator2.remove();}System.out.println(list2);//[a, c]//remove之前不能有add()try {List<String> list4 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator4 = list4.listIterator();while (listIterator4.hasNext()) {if (listIterator4.next().equals("b")) {listIterator4.add("添加");listIterator4.remove();}}} catch (Exception e) {System.out.println("remove之前有add也会报 IllegalStateException");}//移除指定范围内的所有元素List<String> list3 = new LinkedList<String>(Arrays.asList(new String[] { "a", "开始", "b", "c", "d", "结束", "e" }));ListIterator<String> listIterator3 = list3.listIterator();while (listIterator3.hasNext()) {if (listIterator3.next().equals("开始")) {listIterator3.remove();//注释掉这行代码则不移除"开始"while (listIterator3.hasNext()) {if (!listIterator3.next().equals("结束")) {listIterator3.remove();//remove之后必须再调用next方法后才能再remove} else {listIterator3.remove();//注释掉这行代码则不移除"结束"break;//结束while循环}}}}System.out.println(list3);//[a, e]//替换指定元素List<String> list5 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" }));ListIterator<String> listIterator5 = list5.listIterator();while (listIterator5.hasNext()) {if (listIterator5.next().equals("b")) {listIterator5.remove();listIterator5.add("替换");}}System.out.println(list5);//[a, 替换, c]
ListIterator add remove 使用注意的更多相关文章
- WIX: Hide installed program from the Add/Remove Programs window.
Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...
- How to hide an entry in the Add/Remove Programs applet?
Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Prog ...
- Maste Note for OCR / Vote disk Maintenance Operations (ADD/REMOVE/REPLACE/MOVE)
Doc ID 428681.1 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.1 to 11.2.0.1.0 [R ...
- 有关集合的foreach循环里的add/remove
转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...
- HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)
引言:我们都知道HashSet这个类有add remove contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...
- SharePoint自动化系列——Add/Remove "Record" from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 目的:批量的将SharePoint items变成records或者将records变成普通的it ...
- SharePoint自动化系列——Add/Remove “Hold” from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题1: 1.如果SharePoint item被添加了hold,通过UI界面来对SharePoi ...
- 阿里规范学习总结-不要再foreach对元素进行add()/remove()操作,
在foreach循环中,对元素进行 remove()/add() 操作需要使用Iterator ,如果运行在多线程环境下,需要对Iterator对象枷锁. public class ForeachTe ...
- LinkedList add remove get 代码分析
add void linkLast(E e) { //e 要添加的元素 final Node<E> l = last; // 最后一个元素 final Node<E> newN ...
随机推荐
- 正则表达式 U贪婪模式
<?php/*模式修正符号: i u 位置:"//模式修正符位置" 可以一次使用一个,也可以一次使用多个 对整个正则表达式调优用的,也可以说是对正则表达式功能的扩展 &quo ...
- JavaScript-学习一加载不动
为先加载的js后加载的html 加载完js运行时因为未加载html的原因导致找不到js所控制的元素 所以解决的方法就是把js放到控制元素的下方 或者html的底部 做成函数的时候可以放在头部,也就是说 ...
- HTTP协议学习-03
浏览器访问一个网站,的原理图
- IOS制作一个漂亮的登录界面
上图是Facebook的登录界面,看起来很漂亮,eamil框和passwod框合在一起,那么这种效果是怎么做出来的呢?我们都知道输入框用layer属性是可以做成圆角的形式,那么怎么样才能够仅仅只让上边 ...
- codeforces 232D Fence
John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the righ ...
- 1、vs2012 mvc3项目ObjectContext类找不到的问题
在vs2012下找不到ObjectContext类,取而代之的是DBContext,实体对象的Attach方法,上下文的ObjectStateManager对象都找不到,解决办法: 在设计视图中打开E ...
- Chrome extension
PageSpeed Tincr SpriteMe JSONView FireMobileSimulator for Google Chrome™
- How to solve “sudo: /etc/sudoers.d is world writable”
Run pkexec chmod 0440 /etc/sudoers
- Android 4.0 新增的显示数据集的桌面控件
setRemoteAdapter (int viewId, Intent intent):该方法可以使用 Intent 更新 RemoteViews 中viewId 对应的组件. 上面方法的 Inte ...
- Binary Tree Inorder Traversa
package cn.edu.xidian.sselab.hashtable; import java.util.ArrayList;import java.util.List;import ja ...