I'm getting a ConcurrentModificationException here at the line 'for (Node node : nodes', but I can't figure it out; I've been trying different approaches for the last 2 hours, and still can't get it working. I just don't get what's going wrong... it's definitely not the 'do stuff' lines that are the problem, because the Exception is thrown regardless of what's in there.
Does anyone have any idea why it's happening?
public void addHops(Node sourceNode, int numberHops) {
List<Node> newNodeLayer = new ArrayList<Node>();
newNodeLayer.add(sourceNode);
ArrayDeque<List<Node>> queue = new ArrayDeque<List<Node>>();
queue.add(newNodeLayer);
//
// For as many hops as desired
//
for (int i = 0; i < numberHops; i++) {
//
// For as many queue elements as there were when we started on this
// hop
//
Iterator<List<Node>> iterator = queue.iterator();
while (iterator.hasNext()) {
//
// Get the next node list for the hop
//
List<Node> nodes = iterator.next();
//
// For each node within the list
//
// EXCEPTION THROWN HERE
for (Node node : nodes) {
//
// do stuff
//
}
}
}
}
Where the 'do stuff' lines are, I add to the queue another List<Node>. I've tried storing the additions elsewhere and adding the List<Node> elements
after the iteration is complete, and it makes no difference. Hence me thinking those lines aren't the problem.
This is incredibly frustrating! I can't see what could be going wrong.