Exception in thread "main" java.util.ConcurrentModificationException
2016-10-19 21:28
459 查看
今天在修改别的代码的时候遇到这的问题,要删除掉集合里的元素
Java代码
public void operate(List list){
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
list.remove(str);
}
}
}
这种写法一运行就会报如下异常:
Java代码
Exception in thread "main" java.util.ConcurrentModificationException
t java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
因为list在循环中的时候是不可以删除它的元素的。后来我是这样做的,一种很笨的方法,思路是这样的:创建一个List专门存放要被删除的元素,循环过后,用List.removeAll方法把元素删除。代码如下:
Java代码
public void operate(List list){
List removeList= new ArrayList();
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
removeList.add(str);
}
}
list.removeAll(removeList);
}
这样也确实可以解决问题了,但是方法实在太笨重,其实可以有更简单的更高效的方法,就是用Iterator.remove方法,如下:
Java代码
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
it.remove();
}
}
多实践,多累计,技术方面总会有提升的。
Java代码
public void operate(List list){
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
list.remove(str);
}
}
}
这种写法一运行就会报如下异常:
Java代码
Exception in thread "main" java.util.ConcurrentModificationException
t java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
因为list在循环中的时候是不可以删除它的元素的。后来我是这样做的,一种很笨的方法,思路是这样的:创建一个List专门存放要被删除的元素,循环过后,用List.removeAll方法把元素删除。代码如下:
Java代码
public void operate(List list){
List removeList= new ArrayList();
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
removeList.add(str);
}
}
list.removeAll(removeList);
}
这样也确实可以解决问题了,但是方法实在太笨重,其实可以有更简单的更高效的方法,就是用Iterator.remove方法,如下:
Java代码
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("chengang")){
it.remove();
}
}
多实践,多累计,技术方面总会有提升的。
相关文章推荐
- Exception in thread "main" java.util.ConcurrentModificationExceptions
- List删除元素报Exception in thread "main" java.util.ConcurrentModificationException异常,或数据删除不完整
- 异常:Exception in thread "main" java.util.ConcurrentModificationException
- Exception in thread "main" java.util.ConcurrentModificationException
- 遍历集合时出现的异常Exception in thread "main" java.util.ConcurrentModificationException
- 遍历集合时出现的异常Exception in thread "main" java.util.ConcurrentModificationException
- Exception in thread "main" java.util.ConcurrentModificationException
- java -- Exception in thread "main" java.util.ConcurrentModificationException
- Exception in thread "main" java.util.ConcurrentModificationException
- 多线程报错 : Exception in thread "Thread-3" java.util.ConcurrentModificationException 并发修改异常
- Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
- Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
- Exception in thread "Thread-6" java.util.ConcurrentModificationException
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- 包删除Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- 【myeclipse 10 】Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInpu
- 异常:Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream