2016-01-29 51 views
3

我有這樣一段代碼:java.util.ConcurrentModificationException在Groovy

 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。任何想法?

+0

更新與堆棧跟蹤的問題。 – Rao

+0

Groovy 1.5已超過8歲......請儘可能升級 –

回答

2

不只是做

violations = violations.findAll { !it.selected } 
+0

謝謝!它的工作現在! – Walker

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 
} 

}

+0

錯誤:groovy.lang.MissingMethodException:沒有方法簽名:java.util.ArrayList.removeAll() – Walker

+0

您使用的是什麼版本的groovy? – Ulises

+0

我使用的Groovy版本是1.5。 – Walker

相關問題