Problem iterating linked/synchronised list

Hello, I’m working on a project to perform a fast march algorithm in Java (finds the optimum path through a map from any point on the map to a goal) and I’m having a terrible old time trying to get my lists to be iterated correctly. I believe this is to do with the fact that I am attempting to alter the size of the list while I iterate it. I therefore thought I would have to use synchronised lists, which I am attempting to use but which don’t change my runtime errors at all. Some example code of my problem I have listed below, the line where I get a java.util.ConcurrentModificationException is in bold. If I get this working I’ll share the Fast March algorithim code here, I don’t know if it’s of any use to anyone, but it’s an interesting piece of AI, anyway.

You can’t modify a collection while you’re iterating over it, syncronised or not. You should instead be using ListIterator.add and .remove if you need to change it.

Hmm, okay, can you give me some example code of that? Using the code I’ve given - say iterate a near list while adding and removing things from it, if you know what I mean.

Another option is to draw a copy of the list with toArray() and iterate on the array. In contrast to ListIterator, adding/removing to/from the list won’t change current iteration behaviour. Depends on what you need.

[quote]Another option is to draw a copy of the list with toArray() and iterate on the array. In contrast to ListIterator, adding/removing to/from the list won’t change current iteration behaviour. Depends on what you need.
[/quote]
…or even ArrayList.clone(), which returns an ArrayList, rather than dropping out to arrays… (all concrete List implementations should implement Cloneable, you just have to cast to the concrete type before calling it, since List doesn’t extend Cloneable…)

None of the answers have helped me so far, I think this is as I haven’t explained my problem fully. I’ll explain my algorithim, hopefully this should clear up exactly what I’m trying to do.

I have 3 lists - a near, far and known list.
The goal node is in the known list. (it’s ‘travel time’ is known.)
all other nodes in the far list.
1.I add the nodes surrounding the goal node into the near list, removing from far list.
2.I take the first node in the near list, add the surrounding nodes of that to the end of the near list, removing from far.
3.I perform maths on this node until it’s travel time is known.
4.I add this node to the known list, removing from near.
5. I then go back to stage 2 until the near list is empty (far list will also be empty, known list will be full).

As you can see I’m trying to iterate through a near list who’s size is constantly changing. What can I do?

My AI is somewhat shaky, but I don’t think you need to iterate over near at all? You just keep removing nodes from the head of near until it its empty or you’ve found your goal.

Suggest you edit that to describe lists as 1, 2, and 3, or something equally obvious. without serious effort, I can’t follow what you’re trying to say using names near far and known which mean nothing to me.

AFAICS you don’t want to use iterators in at least one place - you aren’t iterating, so don’t use them. It looks like you’re just trying to FIFO-queue a list - you want to take items off the head and add items to the tail. That’s not iterating.

Use linkedlist.removeFirst() addLast() etc.

Yes, this makes sense, I’m not getting the same error, anymore.

I’m getting a different error.

It’s a out of bounds exception, which I’m working on, I’ll come back if I have any other questions.

Thanks…