Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  ConcurrentModificationException  (Read 1674 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-01-18 05:26:42 »

I am getting this exception on my game from using ArrayList. I tried making it a CopyOnWriteArrayList, but I feel like this a glitchy and it is causing problems.

Any thoughts on what to do?

Thanks,
-Nathan

Source

TIL How to make my links look good, and not 50000 characters long.

Check out my website!
Offline Stranger

Senior Member


Medals: 6



« Reply #1 - Posted 2012-01-18 07:07:09 »

you iterate the list in foreach and try to remove element by list.remove in Main:
1  
2  
3  
public void menuInit()
{
   for (Boulder b : boulders) boulders.remove(b);

use 'for' loop with iterator and remove by iterator.

Anton
Offline Waterwolf

Junior Member


Medals: 3



« Reply #2 - Posted 2012-01-18 07:42:43 »

1  
2  
3  
public void menuInit()
{
   for (Boulder b : boulders) boulders.remove(b);


I didn't download the sources but if the loop looks like that you could use boulders.clear() instead of that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-19 06:27:30 »

Removing an item while using the for-each loop will throw a ConcurrentModificationException.

However, I don't know what your exact error is so.....

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2012-01-19 14:12:40 »

I love that there ^^^ haha. And I am at school right now so I can't post a stack trace, but I was indeed using two for loops to cycle through a 2D array and remove boulders that were in holes. Is there a way around this?

-Nathan

Check out my website!
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #5 - Posted 2012-01-19 14:24:48 »

Use an Iterator, unfortunately it's not as pretty as standard foreach.  If you nave an List<Boulder> of boulders, for example

1  
2  
3  
4  
5  
6  
for (Iterator<Boulder> i = boulders.iterator(); i.hasNext(); ) {
   Boulder boulder = i.next();
   if (isInHole(boulder)) {
      i.remove();
   }
}

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-01-19 21:25:22 »

If you need to remove an element at a position other than the last accessed one:
1  
2  
3  
4  
5  
6  
for(int a = 0; a < list.size(); a++) {
   ....
   int i = toRemove();
   list.remove(i);
   if(i <= a) a--;
}

Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-01-19 21:42:00 »

Or yunno, just use Iterator, which is designed for the task, works on collections that aren't sequentially indexed, and actually happens to be threadsafe.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-01-19 22:16:28 »

Threadsafe? Iterators are as threadsafe as their implementation. There are no guarantees.

ConcurrentModificationException has little to do with concurrency (in this case).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Matthias

Senior Member


Medals: 3
Projects: 1


TWL - Themable Widget Library


« Reply #9 - Posted 2012-01-19 22:17:55 »

Iterator is nearly never thread safe. There are only very few collections (eg java.util.concurrent.*) which are thread safe on their own - everything else needs external locking.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline H3rnst

Senior Newbie


Medals: 1



« Reply #10 - Posted 2012-01-20 11:57:56 »

This is my first post!

I don't understand. From what I see here I'm thinking that you want to empty the boulders list.
1  
2  
3  
public void menuInit()
{
   for (Boulder b : boulders) boulders.remove(b);


Have you tried the clear() method?
1  
2  
3  
public void menuInit()
{
    boulders.clear();




Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-01-20 12:29:49 »

Hey H3rnst, welcome to JGO!

Yes, but now we are talking about removing based on a condition. Also it is best to remove all of them manually since clear() doesn't null out the references, thus the GC cannot reclaim the memory.

Offline Matthias

Senior Member


Medals: 3
Projects: 1


TWL - Themable Widget Library


« Reply #12 - Posted 2012-01-20 12:47:21 »

ArrayList.clear() does null out all used slots. Where did you get that false information from?
Offline breno.inojosa

Junior Newbie





« Reply #13 - Posted 2012-01-20 15:46:45 »

The problem is that you're trying to access (meaning iterate) the collection while it is being changed (you're adding or removing elements).

Code below is just scratch.

This can be due to:

for(collection iteration)
  if(x)
    remove element // add element


You can solve this by:

copyOfArray = originalArray.copy
for(originalArray iteration)
  if(x)
     do something (copyOfArray.remove or copyOfArray.add)

if you need to change the other array, just copy the copyOfArray to the original array.

I had the same problem and I solved this using the strategy above.
If you still have any trouble, when I get home I'll post the code.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-01-21 05:30:24 »

ArrayList.clear() does null out all used slots. Where did you get that false information from?
My bad, I thought I read somewhere a while back that clear() doesn't null out the internal array...then I just looked at ArrayList's source Lips Sealed

Offline Matthias

Senior Member


Medals: 3
Projects: 1


TWL - Themable Widget Library


« Reply #15 - Posted 2012-01-21 08:01:22 »

An easy way to remove (or add) entries while iterating over an array (or ArrayList) without using the for-each loop or Iterator is to use array indices and walk backwards through the array:
1  
2  
3  
4  
5  
6  
for(int i=list.size() ; i-->0 ;) {
   Entry e = list.get(i);
   if(someTest(e)) {
      list.remove(i);
   }
}

This works because any modification of the array/list at or after the current index won't affect the rest of the loop.
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.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!