Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem iterating linked/synchronised list  (Read 1253 times)
0 Members and 1 Guest are viewing this topic.
Offline aerisdead

Junior Newbie




www.insertcredi t.com


« Posted 2004-05-04 23:11:50 »

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.


Quote

   private java.util.List near;
   private java.util.List far;
   private java.util.List known;
   terrain curr;

       near = Collections.synchronizedList(new LinkedList());
       far = new LinkedList();
       known = new LinkedList();
       curr =new terrain();

       synchronized(near){
           ListIterator linear = near.listIterator(0);
           while ( linear.hasNext()) {
               curr = (terrain)linear.next();      // selects item within the list being iterated
               
             
               if(curr.pos.x != 0){
                   if(far.contains(map[curr.pos.x-1][curr.pos.y])){
                       near.add(map[curr.pos.x-1][curr.pos.y]);
                       far.remove(map[curr.pos.x-1][curr.pos.y]);
                   }
                   else if(known.contains(map[curr.pos.x-1][curr.pos.y])){
                       ux1= map[curr.pos.x-1][curr.pos.y];
                   }
               }
             
//Bunch of other checks would go here, and a whole bunch
// of maths, and finally the slowness of curr would be
//known, and we'd add it to the known list...

               known.add(curr);
           }
       }
   }


--
Insert Credit - Contemporary youth prepares for the coming revolution
Offline Orangy Tang

JGO Kernel


Medals: 48
Projects: 11


Monkey for a head


« Reply #1 - Posted 2004-05-04 23:27:04 »

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline aerisdead

Junior Newbie




www.insertcredi t.com


« Reply #2 - Posted 2004-05-05 00:19:24 »

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.

--
Insert Credit - Contemporary youth prepares for the coming revolution
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #3 - Posted 2004-05-05 06:28:58 »

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.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #4 - Posted 2004-05-05 09:53:26 »

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.


...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...)

malloc will be first against the wall when the revolution comes...
Offline aerisdead

Junior Newbie




www.insertcredi t.com


« Reply #5 - Posted 2004-05-05 13:35:24 »

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?

--
Insert Credit - Contemporary youth prepares for the coming revolution
Offline Orangy Tang

JGO Kernel


Medals: 48
Projects: 11


Monkey for a head


« Reply #6 - Posted 2004-05-05 13:53:52 »

Quote

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #7 - Posted 2004-05-05 13:55:37 »

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.

malloc will be first against the wall when the revolution comes...
Offline aerisdead

Junior Newbie




www.insertcredi t.com


« Reply #8 - Posted 2004-05-05 15:02:40 »

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...

--
Insert Credit - Contemporary youth prepares for the coming revolution
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (102 views)
2013-05-17 21:29:12

alaslipknot (112 views)
2013-05-16 21:24:48

gouessej (140 views)
2013-05-16 00:53:38

gouessej (136 views)
2013-05-16 00:17:58

theagentd (147 views)
2013-05-15 15:01:13

theagentd (133 views)
2013-05-15 15:00:54

StreetDoggy (176 views)
2013-05-14 15:56:26

kutucuk (198 views)
2013-05-12 17:10:36

kutucuk (200 views)
2013-05-12 15:36:09

UnluckyDevil (206 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.116 seconds with 21 queries.