Check when looping through entities is done.

Hello there,

I am currently having a small issue about ConcurrentModificationException(). I know there are alot of tutorials out there on how to fix this problem, but I only saw evryone had trouble removing entities. I am having trouble adding entities. I thaught I do the same thing as I did with destroying entities. I didnt knew I had this issue, because I was adding entities in the Game.java and not in an entitie. In that way I added the entities before the level was “ticking” through the arrayList of the entities. But now my problem is: I don’t know how to add entities after ticking throught them. This is what I tried:

    public synchronized void tick() {
    	Iterator<Entity> it = getEntities().iterator();
        while (it.hasNext()) {
           Entity e = it.next();
           e.tick();
           
           if (e.destroyed) {
              it.remove();
           }
           
           if (e.spawn) {
        	   Entity s = e.getEntityToSpawn();
        	   this.getEntities().add(s);
        	   e.spawn = false;
           }
        }
    }

But as you can see I am just again adding the entities while looping through the list. Does someone know how to fix this and only explain it. I need to learn java so I want to get the solusion by myself!

Already thanks!
-RoseSlayer