import java.util.*;public class TestList{
public static void main(String args[]){
List<String> li = new ArrayList<String>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
li.add("6");
for (String s : li) {
li.remove(s);
}
for (int i = 0; i < li.size(); i++) {
System.out.println(li.get(i));
}
}
}
用增强的for循环,为什么会抛出ConcurrentModificationException的异常?
解决方案 »
- 跪求,解决方案
- 上次关于一道imun,我还是没明白,谁能帮我讲讲这个流程哈
- 调试java源码
- 如何从BLOB字段获取文件类型?
- 怎样才能让动画GIF在应用程序的一个界面上显示啊?
- 用web方式打开一StringBuffer变量中的数据
- 求圆周率 Pi (π)值,精确到小数点后1000位 ---分不够可以再加
- 哪位可以介绍一下Java经常使用的一些包???
- ##### java 网络编程使用 socks 代理的问题 #####
- 如何用反射生成对象?
- 纠结JAVA SOCKET 异常:java.io.StreamCorruptedException: invalid stream header
- Struts2中关于servlet的问题
for(Iterator<String> it=li.iterator();it.hasNext();){
it.next();
it.remove();
}
http://download.oracle.com/javase/1.5.0/docs/api/java/util/ConcurrentModificationException.html
上面是java-doc上的说明
The remove method removes the last element that was returned by next from the underlying Collection. The remove method may be called only once per call to next and throws an exception if this rule is violated.Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.Use Iterator instead of the for-each construct when you need to: * Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
* Iterate over multiple collections in parallel. The following method shows you how to use an Iterator to filter an arbitrary Collection — that is, traverse the collection removing specific elements. static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}
li.remove(s);
增强for循环只能迭代,不可修改List内容,具体原因看源码,
这是ArrayList中的add方法实现的源码,内部实现是采用数组实现的!而不是采用链表!
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
import java.util.*;public class TestList {
public static void main(String args[]) {
List<String> li = new ArrayList<String>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
li.add("6");
for (String s : li) {
if(s.equals("5")) //这是只能是最后第二个元素的值("5")
li.remove(2);//这里移除哪个元素都可以
}
for (int i = 0; i < li.size(); i++) {
System.out.println(li.get(i));
}
}
}}
不知道有没有人能解释上面的现象。
for(Iterator<String> it=li.iterator();it.hasNext();){
it.next();
it.remove();
}
就是这样,别无它发。
我也F你了, 你不知道 remove 被有 remove(Object o) 这个方法吗?
如果一定要修改。就用Iterator.remove();
list.remove(int index )//可以移除索引
list.remove(Object o)//也可以移除对象
15楼是对的,这样才不会报错
ArrayList底层是数组实现的,假设现在有a[0]=1,a[1]=2,a[2]=3
用for循环遍历的时候
for(int i=0;i<a.length;i++){
}
假设遍历到第二个数 也就是a[1]的时候,删除了a[1] ArrayList的底层是将a[1]后面的数向前移动一个位置,由于下标改了 i没改 这样就会发生一些问题 所以就出现在遍历的时候改变整个数组 就干脆抛异常了用迭代的方式则没这个问题
谁说list遍历进行remove?正序是不行的,因为移除后下一个迭代序号永远比你拥有的要大,反序就可以!
2.用迭代器删
3.用一个临时的tmpList保存要删的东东,for完一起removeAll