Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
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 1534 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: 56
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!
Legends of Yore - The Casual Retro Roguelike
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: 56
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.

 

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

The first screenshot will be displayed as a thumbnail.

SHC (29 views)
2014-07-12 17:50:04

Riven (31 views)
2014-07-10 20:20:18

CopyableCougar4 (30 views)
2014-07-10 02:26:14

CopyableCougar4 (32 views)
2014-07-09 02:55:38

Code Mage (33 views)
2014-07-08 23:57:00

Code Mage (21 views)
2014-07-08 23:49:08

AppleSauce (28 views)
2014-07-08 19:25:32

CopyableCougar4 (29 views)
2014-07-06 01:51:26

ipe369 (35 views)
2014-07-05 14:18:25

vastrolorde (44 views)
2014-07-04 18:45:44
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!