Java.util.ConcurrentModificationException在Groovy中的含义

3

我有这段代码:

       void remove(){
            boolean allowRemove = false;
            violations.each{
                if(it.selected) allowRemove = true;
            }
            if(!allowRemove) throw new Exception("No item selected!");
            if(allowRemove){
                def templist = violations;
                templist.each{ if(it.selected) templist.remove(it) }
                violations = templist;
                tableHandler.reload();
            }
        }

每次执行此代码时,我的应用程序都会抛出一个错误:java.util.ConcurrentModificationException。我在Java中找到了一个解决方法,使用Iterator。但我不知道如何在Groovy中编写它。有任何想法吗?

使用堆栈跟踪更新问题。 - Rao
Groovy 1.5已经超过8年了... 如果可以的话请升级。 - tim_yates
2个回答

3

使用 templist.removeAll{it.selected}

编辑:这适用于 Groovy 1.7.4:

class Test {

static void main(String[] args) {
    def c = [1, 2, 3, 4, 5]
    c.removeAll { it % 2 == 0 }
    println c
}

}


错误:groovy.lang.MissingMethodException: java.util.ArrayList.removeAll()方法的签名不存在。 - Walker
你使用的Groovy版本是什么? - Ulises
我使用的 Groovy 版本是 1.5。 - Walker
1
你能升级吗?removeAll是在1.7.4中引入的。 - Ulises

2

这难道不是只做

violations = violations.findAll { !it.selected }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接