Thursday, 22 August 2013

Is possible to erase an ArrayList element inside a for( ) cycle?

Is possible to erase an ArrayList element inside a for( ) cycle?

pI have this code:/p poutlets is a ArrayList passed to the method;
riverBasin is a 2D matrix of int (int[][] riverBasin);/p precodefor (int[]
item: outlets) { if (item[0] lt; 2 || item[0] gt; this.riverBasin.length -
1 || item[1] lt; 2 || item[1] gt; this.riverBasin[0].length - 1) {
System.out.println(This provisionally substitutes error catching. Outlet (
+ item[0] + , + item[1] + ) is not correct.); outlets.remove(item);
System.out.println(Remaining outlets: ); for (int[] atem: outlets) {
System.out.print(( + atem[0] + , + atem[1] + )\n); } } else {
this.riverBasin[item[0]][item[1]] = 10; } } /code/pre premoving the item
from the ArrayList outlets generate an error:/p precodeException in thread
main java.util.ConcurrentModificationException at
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343) at
org.geoframe.ocn.Eden.setMultipleOutlet(Eden.java:135) at
org.geoframe.ocn.Eden.main(Eden.java:205) /code/pre pwhich I do not really
completely understand. I suspect, however, that I broke the iterator.
Correct ? How could then I remove the unwanted elements in the
ArrayList./p pThank you in advance for any help,/p priccardo/p

No comments:

Post a Comment