The following code is just to produce an example of the problem:
public static void main(String[] args) {
Collection<Integer> src = new ArrayList<Integer>();
Collection<Integer> dest = new ArrayList<Integer>();
src.add(2);
src.add(7);
src.add(3);
src.add(2201);
src.add(-21);
dest.add(10);
while (src.size() != 0) {
for (int i : dest) {
int min = Collections.min(src);
dest.add(min);
src.remove(min);
}
}
}
What I want to do is move everything from src to dest in a specific order. (Here, it's what is the minimum value, but that's just a simplification from my real problem.) However, I am modifying dest while iterating over it, and get the following error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at nth23.experimental.MoveBetweenSets.main(MoveBetweenSets.java:25)
How can I get around this?