package com.yuzhenc.collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test10 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("abc");
list.add("def");
list.add("ghi");
for(Iterator<String> iterator = list.iterator(); iterator.hasNext(); ){
if ("def".equals(iterator.next()))
list.add("jkl");
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8fff57614ffa9fe61f8d97cea527391b.png)
package com.yuzhenc.collection;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Test11 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("abc");
list.add("def");
list.add("ghi");
for(ListIterator<String> iterator = list.listIterator(); iterator.hasNext(); ){
if ("def".equals(iterator.next()))
iterator.add("jkl");
}
System.out.println(list);
}
}