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 (546)
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  
  Array within an array  (Read 336 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Posted 2014-02-28 11:41:54 »

Ok so im writting this game and I so far the world is randomly generated, this means that vegetation(trees, plants etc.)spawn all over the place. To prevent overlapping I created a method that would delete one "vegetation" if its touching another as seen in the source code below:

1  
2  
3  
4  
5  
6  
7  
for (Vegetation c: Object_Control.Vegetationarray) {
         for (Vegetation i: Object_Control.Vegetationarray) {
            if (c.getImageBounds().intersects(i.getImageBounds())) {
               Object_Control.removeVegetation(c);
            }
         }
      }


The "getImageBounds()" method returns a rectangle that is the same size as the image.

The error I seem to be getting is:

1  
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException


I am happy to supply any more source code related to the issue if needed

I love making games...
Offline StrideColossus
« Reply #1 - Posted 2014-02-28 11:55:26 »

I assume Object_Control.Vegetationarray is a collection such as an ArrayList?

Essentially what the for loop is attempting to do is modify the collection at the same time you are iterating over it, hence the exception.

Two ways round this:

1. Get an iterator from the collection and use Iterator.remove()  (not supported by all collections mind and probably not the best approach for this case anyway).

2. Create a new list of objects to be removed and then remove them in a second step.

i.e.

1  
2  
3  
4  
5  
6  
7  
List<Vegetation> removed = new ArrayList<>();
for(...) {
    for(...) {
        if( ... ) removed.add( c );
    }
}
Object_Control.Vegetationarray.removeAll( removed );


- stride
Offline jonnyzGaming

Junior Member


Medals: 3
Projects: 2



« Reply #2 - Posted 2014-02-28 12:16:34 »

In addition to what stride said, with the double enhanced for loop, if i'm not mistaken. You are comparing every vegetation with itself + all others? Wont that mean every vegatation intersects with itself, meaning your deleting all vegetation in the game  Shocked. Try doing a normal double for loops up to arrays size, but in the inner for loop have an if statement to ensure your not comparing the same vegation.
eg if(!c.equals(i) && c.getImageBounds.inter    etc...  Then need to remove it.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #3 - Posted 2014-02-28 12:22:45 »

In addition to what stride said, with the double enhanced for loop, if i'm not mistaken. You are comparing every vegetation with itself + all others? Wont that mean every vegatation intersects with itself, meaning your deleting all vegetation in the game  Shocked. Try doing a normal double for loops up to arrays size, but in the inner for loop have an if statement to ensure your not comparing the same vegation.
eg if(!c.equals(i) && c.getImageBounds.inter    etc...  Then need to remove it.

You were right, it does delete all vegetation. So what were you explaining im having trouble understanding your method

I love making games...
Offline jonnyzGaming

Junior Member


Medals: 3
Projects: 2



« Reply #4 - Posted 2014-02-28 12:26:37 »

Well you would do it like stride said, but just change the inner loop
eg
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
for(int c = 0; Object_Control.Vegetationarray.size(); c++)
  for( i = 0; Object_Control.Vegetationarray.size(); i++)

    //get the two vegations you want to compare.
   Vegation vegation = Object_Control.Vegetationarray.get(i);
    Vegation vegationToCompare = Object_Control.Vegetationarray.get(c);
   
    //compare them, removing if they interect put ensuring they not the same vegation also.
   if(!vegation .equals(vegationToCompare) && vegationToCompare .getImageBounds().intersects(vegationToCompare.getImageBounds()))
    remove.add(i)


This !c.equals(i) ensures that you are not going to remove any vegetation if they are the same.
  
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #5 - Posted 2014-02-28 13:53:39 »


Thanks this worked a treat. Smiley

I love making games...
Pages: [1]
  ignore  |  Print  
 
 

 

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 (21 views)
2014-08-19 09:29:53

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

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

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

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

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

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

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

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

BurntPizza (65 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!