ackage com.itheima;
import java.util.ArrayList;
import java.util.List;
/*
* 第2题:一个ArrayList对象aList中存有若干个字符串元素,
* 现欲遍历该ArrayList对象,删除其中所有值为"abc"的字符串元素,
* 请用代码实现。*/
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> alist=new ArrayList<String>();
alist.add("abc");
alist.add("abd");
alist.add("abe");
alist.add("abc");
alist.add("abc");
alist.add("cba");
//System.out.println(alist);
for(Object element:alist){
if("abc".equals(element)){
alist.remove(element);
}
}
System.out.println(alist);
}
}
/*Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at com.itheima.Test2.main(Test2.java:28)*/
|
|