Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  ConcurrantModificationException  (Read 1391 times)
0 Members and 1 Guest are viewing this topic.
Offline aufde

Senior Newbie





« Posted 2012-12-31 06:41:36 »

Here's my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
   public ArrayList<PlayerCharacter> goodGuyEntities = new ArrayList<PlayerCharacter>();
   public ArrayList<BadGuy> badGuyEntities = new ArrayList<BadGuy>();
   public ArrayList<Shot> goodGuyShotEntities = new ArrayList<Shot>();
   public ArrayList<Shot> badGuyShotEntities = new ArrayList<Shot>();
   public ArrayList<Shot> removeList = new ArrayList<Shot>();

      for (PlayerCharacter character : goodGuyEntities) {
         character.update();
      }

      for (BadGuy badGuy : badGuyEntities) {
         badGuy.update();
      }

      for (Shot shot : goodGuyShotEntities) { // this is the line that eclipse said threw the exception
        shot.update();
         for (BadGuy badGuy : badGuyEntities) {
            if (shot.bounds.intersects(badGuy.bounds)) {
               shot.hit(badGuy);
            }
         }
      }

      for (Shot shot : badGuyShotEntities) {
         shot.update();
         for (PlayerCharacter character : goodGuyEntities) {
            if (shot.bounds.intersects(character.bounds)) {
               shot.hit(character);
            }
         }
      }

      for (Entity remove : removeList) {
         removeEntity(remove);
      }
   

This threw a ConcurrantModificationException and as far as I understand the exception, it's because I'm iterating through the ArrayLists and calling update() on each object in the ArrayList within the iteration. Is there any way to get around this besides using the traditional for loop:

1  
for(int i = 0; i < goodGuyShotEntities.size(); i++)

this fixes it but I don't like how it looks and is much harder to use and read.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-12-31 07:10:37 »

You are not allowed to add or remove any objects from an ArrayList while it is being iterated. That is why you are getting a ConcurrentModificationException.

You most likely remove the "Shot" object from the "goodGuyShotEntities" list inside the hit method, when it intersects with a "BadGuy". Instead, you should use this ping-pong technique described here in lines 16-30: http://www.java-gaming.org/topics/arraylist-vs-linkedlist/27016/msg/240151/view.html#msg240151

So "Shot"-s should have an "isAlive" boolean field that you set to false inside the hit method. This way the loop will know not to add it to the other list.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2012-12-31 15:14:27 »

I personally just use the simple for-loop  persecutioncomplex

I think it is a lot easier to just use that line of code (which shouldn't confuse you at all), and not have to worry about multiple lists and the like.

On another note, I don't agree with your use of a removal list, I would just use the boolean like ra4king recommended.

And also, look into having a single list if you can, it is much cleaner and often easier to handle as your game becomes more complicated (less loops).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2013-01-01 01:05:16 »

I personally just use the simple for-loop  persecutioncomplex
The simple for-loop is flawed in this situation because once you remove an item from the list, unless you properly deal with the index value, you will skip over the next item due to everything being shifted over.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2013-01-01 02:16:19 »

yeah, I guess, but you could just do a counter-- if it is critical. while not optimal, still easier than making another list.
Offline aufde

Senior Newbie





« Reply #5 - Posted 2013-01-02 06:54:40 »

What would be the advantage of using boolean isAlive over a remove list? As it is now, with the simple for loop and the remove list, the objects aren't removed in the hit() method, they are added to the remove list which is then handled in a different loop so the index doesn't change within a loop.

The reason I'm using 4 different arrayLists for all of the entities is because it's a part of brute force collision detection and I figured the less rectangles that needed to be compared, the easier it would be on the system. I just assumed this would make the game run faster, please correct me if I'm wrong.
Offline philfrei
« Reply #6 - Posted 2013-01-03 20:44:49 »

There are some situations where using a "CopyOnWriteArrayList" would solve the problem. A lot depends on the ratio of iteration vs deletes/inserts, and on how much trouble you might get into working from a snapshot of the ArrayList rather than the ArrayList itself.

More info here:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
It is also discussed in the concurrency section of the Java Tutorials.

Alternatively, I seem to recall princec explaining this method (but can't find the original post):
(1) make a copy the ArrayList
(2) as you go through the originals, if there is something to delete, do it to the copy
(3) once the iteration is done, make the copy the new ArrayList.

Marking objects for live/dead in the collision pass makes a LOT of sense, combined with alternating the collision checks with the cleanups. Otherwise, if you start synchronizing the objects to prevent the concurrency error, the resulting blocking could really slow things down.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline theagentd
« Reply #7 - Posted 2013-01-03 21:54:12 »

Alternatively, I seem to recall princec explaining this method (but can't find the original post):
Instead, you should use this ping-pong technique described here in lines 16-30: http://www.java-gaming.org/topics/arraylist-vs-linkedlist/27016/msg/240151/view.html#msg240151
Also, he doesn't copy the ArrayList, he just creates two of them at the start.

Myomyomyo.
Offline philfrei
« Reply #8 - Posted 2013-01-03 22:54:24 »

Thanks theagentd! I should have looked at ra4king's link from the start.

Your code solution pretty much matches princec's advice a couple posts later, yes?
http://www.java-gaming.org/topics/arraylist-vs-linkedlist/27016/msg/240205/view.html#msg240205

I still like the alternative idea of making a copy of the screen that holds ID's instead of graphics, and updating and consulting that instead of brute force checking or maintaining and consulting oct trees or bins. It certainly has its inefficient aspects, but has a sort of neat simplicity: you check the space you want to go to and see if it is occupied or not, and if it is, who is occupying it. Never mind the factorial cross-checking of object against object.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline chrisethompson

Senior Newbie


Medals: 1
Exp: 5 years



« Reply #9 - Posted 2013-01-07 23:33:31 »

I would use the standard for loop.  You get very used to the format of it.  I save iterators for hashMaps and other collections that are not easy to traverse with a simple loop. 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #10 - Posted 2013-01-08 01:28:48 »

Ooh, a con-currant modification exception. Sounds delicious.


See my work:
OTC Software
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2013-01-08 01:54:55 »

Eli you're back!

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #12 - Posted 2013-01-08 19:22:53 »

Eli you're back!
You know, off and on. Busy busy busy. Trying to get a game out there while working full time and being married leaves little room for JGO.

See my work:
OTC Software
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #13 - Posted 2013-01-08 22:00:28 »

Ooh, a con-currant modification exception. Sounds delicious.
I came here to do this. Sad

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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (61 views)
2014-07-12 17:50:04
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!