Add something to arraylist while checking it

Tautvydas Jalinskas

I need to check through an arraylist of "tiles" and if it finds a tile that its not used yet add 4 tiles around that tile but when i add something new while checking the list it crashes. How to add objects to arraylist without crashing it while checking it?

I add one tile and then check it:

private static List<Tile> tiles = new ArrayList<Tile>();



tiles.add(new Tile(mapsize/2*tilesize, mapsize/2*tilesize));

    for(Tile tile: tiles){
        if(!tile.spread){
            tile.spread=true;
            tiles.add(new Tile(tile.position.x-tilesize, tile.position.y)); //this line crashes it
        }
    }
fge

when i add something new while checking the list it crashes

With a ConcurrentModificationException. It is expected. Apart from using an Iterator's .remove() you cannot modify a list like you do (not a non concurrent one like ArrayList anyway).

You should create a new list in which you add your new tiles, then when you're done iterating, addAll this new list to the existing list:

final List<Tile> newTiles = new ArrayList<>();

for (final Tile tile: tiles) {
    if (tile.spread)
        continue;
    tile.spread = true;
    newTiles.add(new Tile(tile.position.x - tilesize, tile.position.y));
}

tiles.addAll(newTiles);

Of course, the code above supposed that your code is not called from several threads simultaneously! Or that this code is in a method protected by a lock...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking For Multiple Collisions While Iterating Through an ArrayList in Java

From Dev

How to use ArrayList while adding something to another Class's constructor ?

From Java

Checking whether something is iterable

From Dev

Checking if something is empty

From Dev

Java - Add elements to an ArrayList while browsing it

From Dev

Add objects in ArrayList while designing class in Java

From Dev

add in arraylist automaticlly and error while try to clear() arraylist

From Dev

Checking if the input is a number or something else

From Dev

Checking if string is not equal to something is not working

From Dev

Checking empty string in an arraylist

From Dev

Checking for duplicates in arraylist

From Dev

Everytime I add something to the arraylist, all previous elements get replaced by the new element, what is happening?

From Dev

While loops checking with time

From Dev

ArrayList, checking size on 2 threads

From Dev

Checking collision for each cube in ArrayList

From Dev

Adding words and checking in Arraylist Hangman

From Dev

Checking whether something exists in a Javascript object with Jquery

From Dev

Issue with my algorithm for checking if something is adjacent to a point

From Dev

Checking if customer has already bought something in WooCommerce

From Dev

Checking whether something exists in a Javascript object with Jquery

From Dev

Bukkit Java NullPointer when checking if something is null

From Dev

Add ArrayList into another ArrayList

From Dev

Add ArrayList in ArrayList and retrieve it

From Dev

how to Add ArrayList in ArrayList

From Dev

How to add something to “basename”?

From Dev

Add something after an argument?

From Dev

checking unique while inserting to mongoDB

From Dev

While loop and checking static variable

From Dev

While loop not checking condition if there are no statements

Related Related

  1. 1

    Checking For Multiple Collisions While Iterating Through an ArrayList in Java

  2. 2

    How to use ArrayList while adding something to another Class's constructor ?

  3. 3

    Checking whether something is iterable

  4. 4

    Checking if something is empty

  5. 5

    Java - Add elements to an ArrayList while browsing it

  6. 6

    Add objects in ArrayList while designing class in Java

  7. 7

    add in arraylist automaticlly and error while try to clear() arraylist

  8. 8

    Checking if the input is a number or something else

  9. 9

    Checking if string is not equal to something is not working

  10. 10

    Checking empty string in an arraylist

  11. 11

    Checking for duplicates in arraylist

  12. 12

    Everytime I add something to the arraylist, all previous elements get replaced by the new element, what is happening?

  13. 13

    While loops checking with time

  14. 14

    ArrayList, checking size on 2 threads

  15. 15

    Checking collision for each cube in ArrayList

  16. 16

    Adding words and checking in Arraylist Hangman

  17. 17

    Checking whether something exists in a Javascript object with Jquery

  18. 18

    Issue with my algorithm for checking if something is adjacent to a point

  19. 19

    Checking if customer has already bought something in WooCommerce

  20. 20

    Checking whether something exists in a Javascript object with Jquery

  21. 21

    Bukkit Java NullPointer when checking if something is null

  22. 22

    Add ArrayList into another ArrayList

  23. 23

    Add ArrayList in ArrayList and retrieve it

  24. 24

    how to Add ArrayList in ArrayList

  25. 25

    How to add something to “basename”?

  26. 26

    Add something after an argument?

  27. 27

    checking unique while inserting to mongoDB

  28. 28

    While loop and checking static variable

  29. 29

    While loop not checking condition if there are no statements

HotTag

Archive