Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  Modified Concurrent Exception  (Read 2443 times)
0 Members and 1 Guest are viewing this topic.
Offline spyder

Junior Newbie





« Posted 2007-03-05 20:01:49 »

What is the preferred way to iterate of a collection to modify its elements?

I have only found two solutions:
1) Create a new collection, then select addAll(). After that iterate the new collection
2) Use org.apache.commons.collections.list.CursorableLinkedList
This allows modifications of the list while iterating.

Are there any other solutions? And which is the most preferable?
Because I guess if all work its mainly a performance question.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2007-03-05 20:30:05 »

When you iterate over a Collection with an Iterator, and remove elements, you should get a "Modified Concurrent Exception" as the title suggests.


To answer your question: it depends.

Variables:
 - type of collection
 - size of collection
 - usage of collection

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

Junior Newbie





« Reply #2 - Posted 2007-03-05 22:22:10 »

Quote
When you iterate over a Collection with an Iterator, and remove elements, you should get a "Modified Concurrent Exception" as the title suggests.

I understand, but I need to loop thru some objects to perform cleanup. As an object cleans up it removes itself from its parent causing the exception.

Quote
Variables:
 - type of collection
 - size of collection
 - usage of collection

My most common collections are:
ArrayList or HashSet with approximately 1-20 elements. There is a fair deal of insert and removal, but getting them is most common ofc.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2007-03-05 22:31:26 »

Have you tried the 'Bag' collection?

It might be exactly what you need.

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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

public class Bag<E>
{
   private final E[] array;
   private int       count;

   public Bag(E e, int capacity)
   {
      this.array = (E[]) Array.newInstance(e.getClass(), capacity);
   }

   public final int count()
   {
      return count;
   }

   public final void put(E e)
   {
      // no checks... don't grow, just deal with the current capacity
      array[count++] = e;
   }

   public final E swap(int index, E e)
   {
      E org = array[index];
      array[index] = e;
      return org;
   }

   public final E grab(int index)
   {
      return array[index];
   }

   public final int search(E e)
   {
      for (int i = 0; i < count; i++)
         if (array[i] == e)
            return i;
      return -1;
   }

   public final void dispose(int index)
   {
      array[index] = array[--count];
      array[count + 1] = null;
   }

   public final void sort(Comparator<E> c)
   {
      Arrays.sort(array, 0, count, c);
   }
}

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

Junior Newbie





« Reply #4 - Posted 2007-03-05 22:37:57 »

I don't think I can use it, as I don't know the capacity.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2007-03-05 22:43:35 »

To answer your original question then:

ArraysLists are extremely quick to copy around, so that's probably the way to go, don't use iterators though, direct access with get( i ) and remove( i ) is much faster. Enhanced-for is evil.

HashMaps/Sets are fairly expensive to construct and fill, lookup will only be faster than ArrayLists on fairly big collections (~64+ elements or so) especially when the internals are building linkedlists (due to overlapping hashes)

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

Junior Duke




Java games rock!


« Reply #6 - Posted 2007-03-06 06:59:48 »

If you just want to get rid of the concurrent modification exception you can also use Iterator#remove.
1  
2  
3  
4  
5  
6  
7  
8  
Collection c;
Iterator i = c.iterator();
while(i.hasNext()) {
  Object o = i.next();
  if (/*should remove o*/) {
    i.remove();
  }
}
Offline spyder

Junior Newbie





« Reply #7 - Posted 2007-03-06 09:48:04 »

Ok, taking all advice into consideration I ended up with this.
What do you think?

1  
2  
3  
4  
5  
6  
7  
public void reset(){
   Object actorA[] = actors.toArray();
   for(int i=0;i<actorA.length;i++){
      final Actor a = (Actor) actorA[i];  
      a.unload();
   }
}
Offline Eliwood

Junior Duke




Stencyl


« Reply #8 - Posted 2007-03-07 18:36:19 »

To answer your original question then:

ArraysLists are extremely quick to copy around, so that's probably the way to go, don't use iterators though, direct access with get( i ) and remove( i ) is much faster. Enhanced-for is evil.

HashMaps/Sets are fairly expensive to construct and fill, lookup will only be faster than ArrayLists on fairly big collections (~64+ elements or so) especially when the internals are building linkedlists (due to overlapping hashes)

How much slower are we talking about? I use iterators all over my code because they're cleaner, and because I actually iterate through all elements.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2007-03-07 21:36:48 »

factor 3, overhead.

For LinkedLists, you definitely want Iterators though.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #10 - Posted 2007-03-14 22:14:37 »

I usually do this when I want to delete elemets from a List:
1  
2  
3  
4  
5  
6  
7  
public void reset(){
   for (int i=actors.size()-1;i>=0;i--){
                if (/*elemnt should be deleted*/) {
                      actors.remove(i);
                }
   }
}


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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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