标签:image drag ide tle LLC foreach 异常 iterable 允许
public interface java.lang.Iterable<T>
public interface java.lang.Iterable<T>
public interface Iterator<E>
public interface Iterator<E>
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
System.out.println(iterator.hasNext() + " " + iterator.hasNext() + " " + iterator.hasNext() + " " + iterator.hasNext());//true true true true
System.out.println(iterator.next() + " " + iterator.next() + " " + iterator.next());//a b c
System.out.println(iterator.hasNext()); //false
System.out.println(iterator.next()); //NoSuchElementException
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
System.out.println(iterator.hasNext() + " " + iterator.hasNext() + " " + iterator.hasNext() + " " + iterator.hasNext());//true true true true
System.out.println(iterator.next() + " " + iterator.next() + " " + iterator.next());//a b c
System.out.println(iterator.hasNext()); //false
System.out.println(iterator.next()); //NoSuchElementException
new ArrayList<String>(Arrays.asList("a", "b", "c")).iterator().remove(); //IllegalStateException
new ArrayList<String>(Arrays.asList("a", "b", "c")).iterator().remove(); //IllegalStateException
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
System.out.println(iterator.next());//a
iterator.remove();
System.out.println(list);//[b, c]
iterator.next();
iterator.next();
iterator.remove();
System.out.println(list);//[b],移除的是最后一次调用 next 时返回的元素
iterator.remove();//IllegalStateException。虽然之前调用了两次 next,但仍然只能调用一次 remove 方法
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
System.out.println(iterator.next());//a
iterator.remove();
System.out.println(list);//[b, c]
iterator.next();
iterator.next();
iterator.remove();
System.out.println(list);//[b],移除的是最后一次调用 next 时返回的元素
iterator.remove();//IllegalStateException。虽然之前调用了两次 next,但仍然只能调用一次 remove 方法
public interface ListIterator<E> extends Iterator<E>
public interface ListIterator<E> extends Iterator<E>
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
System.out.println(iterator.hasPrevious() + " " + iterator.hasNext() + " " + iterator.hasPrevious());//false true false
System.out.println(iterator.next() + " " + iterator.next() + " " + iterator.previous());//a b b,交替调用 next 和 previous 将重复返回相同的元素
iterator.remove();
System.out.println(list);//[a, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
System.out.println(iterator.hasPrevious() + " " + iterator.hasNext() + " " + iterator.hasPrevious());//false true false
System.out.println(iterator.next() + " " + iterator.next() + " " + iterator.previous());//a b b,交替调用 next 和 previous 将重复返回相同的元素
iterator.remove();
System.out.println(list);//[a, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
iterator.add("add1");//该元素直接插入到 next 返回的下一个元素的前面,而 next返回的下一个元素是 a
//因为 previous 没有元素,所以不能用 previous 的规则去解释
iterator.add("add2");//该元素直接插入到 next 返回的下一个元素的前面,而 next返回的下一个元素是 a
//同时也可以这样解释:该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是 add1
System.out.println(list);//[add1, add2, a, b, c]
System.out.println(iterator.next()); //a
System.out.println(iterator.previous() + " " + iterator.previous());//a add2
iterator.add("add3");//该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是 add1
//同时也可以这样解释:该元素直接插入到 next 返回的下一个元素的前面,而 next 返回的下一个元素是 add2
iterator.add("add4");//该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是刚刚通过 add 添加的元素 add3
//同时也可以这样解释:该元素直接插入到 next 返回的下一个元素的前面,而 next 返回的下一个元素是 add2
System.out.println(list);//[add1, add3, add4, add2, a, b, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
iterator.add("add1");//该元素直接插入到 next 返回的下一个元素的前面,而 next返回的下一个元素是 a
//因为 previous 没有元素,所以不能用 previous 的规则去解释
iterator.add("add2");//该元素直接插入到 next 返回的下一个元素的前面,而 next返回的下一个元素是 a
//同时也可以这样解释:该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是 add1
System.out.println(list);//[add1, add2, a, b, c]
System.out.println(iterator.next()); //a
System.out.println(iterator.previous() + " " + iterator.previous());//a add2
iterator.add("add3");//该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是 add1
//同时也可以这样解释:该元素直接插入到 next 返回的下一个元素的前面,而 next 返回的下一个元素是 add2
iterator.add("add4");//该元素直接插入到 previous 返回的下一个元素之后,而 previous 返回的下一个元素是刚刚通过 add 添加的元素 add3
//同时也可以这样解释:该元素直接插入到 next 返回的下一个元素的前面,而 next 返回的下一个元素是 add2
System.out.println(list);//[add1, add3, add4, add2, a, b, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
iterator.add("add1");
System.out.println(iterator.next() + " " + iterator.next()); //a b
iterator.set("set");
System.out.println(list);//[add1, a, set, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
iterator.add("add1");
System.out.println(iterator.next() + " " + iterator.next()); //a b
iterator.set("set");
System.out.println(list);//[add1, a, set, c]
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
System.out.println(iterator.nextIndex() + " " + iterator.nextIndex() + " " + iterator.previousIndex());//0 0 -1
iterator.add("add1");
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//1 0
System.out.println(iterator.next() + " " + list); //a [add1, a, b, c]
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//2 1
System.out.println(iterator.previous() + " " + iterator.previous());//a add1
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//0 -1
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
ListIterator<String> iterator = list.listIterator();
System.out.println(iterator.nextIndex() + " " + iterator.nextIndex() + " " + iterator.previousIndex());//0 0 -1
iterator.add("add1");
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//1 0
System.out.println(iterator.next() + " " + list); //a [add1, a, b, c]
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//2 1
System.out.println(iterator.previous() + " " + iterator.previous());//a add1
System.out.println(iterator.nextIndex() + " " + iterator.previousIndex());//0 -1
List<String> list1 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator1 = list1.listIterator();
iterator1.add("D");
iterator1.add("E");
System.out.println(list1);//[D, E, a, b, c]
List<String> list1 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator1 = list1.listIterator();
iterator1.add("D");
iterator1.add("E");
System.out.println(list1);//[D, E, a, b, c]
List<String> list2 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator2 = list2.listIterator();
while (iterator2.hasNext()) {
iterator2.next();
}
iterator2.add("D");
iterator2.add("E");
System.out.println(list2);//[a, b, c, D, E]
List<String> list2 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator2 = list2.listIterator();
while (iterator2.hasNext()) {
iterator2.next();
}
iterator2.add("D");
iterator2.add("E");
System.out.println(list2);//[a, b, c, D, E]
List<String> list3 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator3 = list3.listIterator();
while (iterator3.hasNext()) {
String item = list3.get(iterator3.nextIndex());
iterator3.add(item + "前面1");
iterator3.add(item + "前面2");
iterator3.next();
iterator3.add(item + "后面1");
iterator3.add(item + "后面2");
}
System.out.println(list3);//[a前面1, a前面2, a, a后面1, a后面2, b前面1, b前面2, b, b后面1, b后面2, c前面1, c前面2, c, c后面1, c后面2]
List<String> list3 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator3 = list3.listIterator();
while (iterator3.hasNext()) {
String item = list3.get(iterator3.nextIndex());
iterator3.add(item + "前面1");
iterator3.add(item + "前面2");
iterator3.next();
iterator3.add(item + "后面1");
iterator3.add(item + "后面2");
}
System.out.println(list3);//[a前面1, a前面2, a, a后面1, a后面2, b前面1, b前面2, b, b后面1, b后面2, c前面1, c前面2, c, c后面1, c后面2]
List<String> list4 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator4 = list4.listIterator();
while (iterator4.hasNext()) {
String item = iterator4.next();
if (item.equals("a") || item.equals("c")) {
iterator4.previous();//先重新指向 item 的前面,这里不用担心 NoSuchElementException
iterator4.add(item + "前面1");//在前面添加元素,添加后还是指向的 item 的前面
iterator4.add(item + "前面2");//在前面添加元素,添加后还是指向的 item 的前面
iterator4.next();//向后【再】移动一位,现在指向的是 item 的后面
iterator4.add(item + "后面1");//在 item 的后面添加元素
iterator4.add(item + "后面2");//在 item 的后面添加元素
}
}
System.out.println(list4);//[a前面1, a前面2, a, a后面1, a后面2, b, c前面1, c前面2, c, c后面1, c后面2]
List<String> list4 = new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" }));
ListIterator<String> iterator4 = list4.listIterator();
while (iterator4.hasNext()) {
String item = iterator4.next();
if (item.equals("a") || item.equals("c")) {
iterator4.previous();//先重新指向 item 的前面,这里不用担心 NoSuchElementException
iterator4.add(item + "前面1");//在前面添加元素,添加后还是指向的 item 的前面
iterator4.add(item + "前面2");//在前面添加元素,添加后还是指向的 item 的前面
iterator4.next();//向后【再】移动一位,现在指向的是 item 的后面
iterator4.add(item + "后面1");//在 item 的后面添加元素
iterator4.add(item + "后面2");//在 item 的后面添加元素
}
}
System.out.println(list4);//[a前面1, a前面2, a, a后面1, a后面2, b, c前面1, c前面2, c, c后面1, c后面2]
List<String> list1 = new ArrayList<String>(Arrays.asList(new String[] { "a", "b", "c", "b" }));
ListIterator<String> iterator1 = list1.listIterator();
while (iterator1.hasNext()) {
if (iterator1.next().equals("b")) {
iterator1.remove();
iterator1.add("替换");
}
}
System.out.println(list1);//[a, 替换, c, 替换]
List<String> list1 = new ArrayList<String>(Arrays.asList(new String[] { "a", "b", "c", "b" }));
ListIterator<String> iterator1 = list1.listIterator();
while (iterator1.hasNext()) {
if (iterator1.next().equals("b")) {
iterator1.remove();
iterator1.add("替换");
}
}
System.out.println(list1);//[a, 替换, c, 替换]
List<String> list2 = new ArrayList<String>(Arrays.asList(new String[] { "a1", "a2", "a3", "b", "c1", "c2", "c3" }));
ListIterator<String> iterator2 = list2.listIterator();
while (iterator2.hasNext()) {
String item = iterator2.next();
if (item.equals("b")) {
System.out.println("当前元素 " + item);
iterator2.remove(); //移除当前元素
if (iterator2.hasPrevious()) {
System.out.println("前 1 个元素 " + iterator2.previous()); //移除此元素的前 1 个元素
iterator2.remove();
}
if (iterator2.hasPrevious()) {
System.out.println("前 2 个元素 " + iterator2.previous()); //移除此元素的前 2 个元素
iterator2.remove();
}
if (iterator2.hasNext()) {
System.out.println("后 1 个元素 " + iterator2.next()); //移除此元素的后 1 个元素
iterator2.remove();
}
if (iterator2.hasNext()) {
System.out.println("后 2 个元素 " + iterator2.next()); //移除此元素的后 2 个元素
iterator2.remove();
}
}
}
System.out.println(list2);//[a1, c3]
//如果 item 为 a1 ,结果为 [b, c1, c2, c3]
//如果 item 为 a2 ,结果为 [c1, c2, c3]
//如果 item 为 c2 ,结果为 [a1, a2, a3]
x
List<String> list2 = new ArrayList<String>(Arrays.asList(new String[] { "a1", "a2", "a3", "b", "c1", "c2", "c3" }));
ListIterator<String> iterator2 = list2.listIterator();
while (iterator2.hasNext()) {
String item = iterator2.next();
if (item.equals("b")) {
System.out.println("当前元素 " + item);
iterator2.remove(); //移除当前元素
if (iterator2.hasPrevious()) {
System.out.println("前 1 个元素 " + iterator2.previous()); //移除此元素的前 1 个元素
iterator2.remove();
}
if (iterator2.hasPrevious()) {
System.out.println("前 2 个元素 " + iterator2.previous()); //移除此元素的前 2 个元素
iterator2.remove();
}
if (iterator2.hasNext()) {
System.out.println("后 1 个元素 " + iterator2.next()); //移除此元素的后 1 个元素
iterator2.remove();
}
if (iterator2.hasNext()) {
System.out.println("后 2 个元素 " + iterator2.next()); //移除此元素的后 2 个元素
iterator2.remove();
}
}
}
System.out.println(list2);//[a1, c3]
//如果 item 为 a1 ,结果为 [b, c1, c2, c3]
//如果 item 为 a2 ,结果为 [c1, c2, c3]
//如果 item 为 c2 ,结果为 [a1, a2, a3]
List<String> list2 = new ArrayList<String>(Arrays.asList(new String[] { "a1", "a2", "a3", "b", "c1", "c2", "c3", "b" }));
x
List<String> list2 = new ArrayList<String>(Arrays.asList(new String[] { "a1", "a2", "a3", "b", "c1", "c2", "c3", "b" }));
List<String> list3 = new ArrayList<String>(Arrays.asList(new String[] { "a", "开始", "b", "c", "d", "结束", "e" }));
ListIterator<String> iterator3 = list3.listIterator();
while (iterator3.hasNext()) {
if (iterator3.next().equals("开始")) {
iterator3.remove();//注释掉这行代码则不移除"开始"
while (iterator3.hasNext()) {
if (!iterator3.next().equals("结束")) {
iterator3.remove();//remove之后必须再调用next方法后才能再remove
} else {
iterator3.remove();//注释掉这行代码则不移除"结束"
break;//结束while循环
}
}
}
}
System.out.println(list3);//[a, e]
List<String> list3 = new ArrayList<String>(Arrays.asList(new String[] { "a", "开始", "b", "c", "d", "结束", "e" }));
ListIterator<String> iterator3 = list3.listIterator();
while (iterator3.hasNext()) {
if (iterator3.next().equals("开始")) {
iterator3.remove();//注释掉这行代码则不移除"开始"
while (iterator3.hasNext()) {
if (!iterator3.next().equals("结束")) {
iterator3.remove();//remove之后必须再调用next方法后才能再remove
} else {
iterator3.remove();//注释掉这行代码则不移除"结束"
break;//结束while循环
}
}
}
}
System.out.println(list3);//[a, e]
标签:image drag ide tle LLC foreach 异常 iterable 允许
原文地址:https://www.cnblogs.com/baiqiantao/p/9311236.html