3D-kreativ
3D-kreativ

Reputation: 9301

Positions in ArrayLists

My code is not working like I thought it should. The list listColumn0 contains the X and Y positions for 8 sprite objects on the screen. When I touch on one of them I check witch sprite object that match that X and Y position and then remove it from the list. But the strange thing is that this only works if I first touch on the last sprite object with index 7 and if I then continue with sprite object with index 6 and so on.

If I click on, let's say, sprite object with index 3 or some of the other besides the last, then the app is closing down! Why this? Can someone see what I have done wrong or can I do this in a better way? Is there a better way to detect/match which sprite object I have touched?

        String size = Integer.toString(listColumn0.size());
    // Check all lists
    for(ColorObject colorObject: listColumn0) {
        if(x > (colorObject.xPosition - colorObject.radius) && x < (colorObject.xPosition + colorObject.radius) && y > (colorObject.yPosition - colorObject.radius) && y < (colorObject.yPosition + colorObject.radius)) {

            String colorCode = Integer.toString(colorObject.color);
            String index = Integer.toString(listColumn0.indexOf(colorObject));
            Log.i("Test","Match!! " + size + " Color: " + colorCode + "ID: " + index);

            listColumn0.remove(listColumn0.indexOf(colorObject));
        }
    }

EDIT:

Eror message from LogCat:

05-22 07:08:55.482: W/dalvikvm(1444): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
05-22 07:08:55.482: E/AndroidRuntime(1444): FATAL EXCEPTION: Thread-124
05-22 07:08:55.482: E/AndroidRuntime(1444): java.util.ConcurrentModificationException
05-22 07:08:55.482: E/AndroidRuntime(1444):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at com.test.game.ColorObjectManager.checkPosition(ColorObjectManager.java:164)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at com.test.game.GameLoop.run(GameLoop.java:190)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at java.lang.Thread.run(Thread.java:856)
05-22 07:13:55.753: I/Process(1444): Sending signal. PID: 1444 SIG: 9

Upvotes: 0

Views: 84

Answers (2)

Kai
Kai

Reputation: 39632

You cannot modify listColumn0 while iterating it with a foreach loop. Doing this results in the ConcurrentModificationException which you can see in the LogCat.

You can modify a Collection while iterating it if you use a old-fashioned Iterator:

Iterator<ColorObject> it = listColumn0.iterator();
while(it.hasNext()) {
   ColorObject colorObject = it.next();
   ...
   it.remove(); // this removes the current object
}

And to reduce the scope of it it is a best practice to use a for loop here:

for (Iterator<ColorObject> it = listColumn0.iterator(); it.hasNext();) {
   ColorObject colorObject = it.next();
   ...
   it.remove(); // this removes the current object
}

Upvotes: 1

rajdeep
rajdeep

Reputation: 59

Easiest solution is

Integer colorObjectIndex = -1;
for(..)
.....
   colorObjectIndex = listColumn0.indexOf(colorObject)
.....
}
// check for colorObjectIndex > -1
listColumn0.indexOf(colorObjectIndex);

Or else you have to use Iterator of listColumn0.

Upvotes: 0

Related Questions