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]
原文:http://www.cnblogs.com/baiqiantao/p/5449434.html