Exception in thread "main" java.util.ConcurrentModificationException Squash the PC dirties the room Violet. The room state is now dirty Lily the animal growls The Animal Lily left the room and goes to Green through the west door. at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at homework5.Room.critReactRoomStateChange(Room.java:76) at homework5.PC.play(PC.java:121) at homework5.Main.main(Main.java:41) Java Result: 1
This is the error I am getting.
My method looks like
public void critReactRoomStateChange(String command, PC pc) { Creature temp = null; Iterator iterator = getCreatures().keySet().iterator(); while (iterator.hasNext()) { String names = iterator.next().toString(); if (!(getCreatures().get(names) instanceof PC)) { temp = getCreatures().get(names); if (temp != null) { temp.reactStateChange(command, pc); temp.checkNewRoom(); } } } }
So, I understand, this means that I resize the iterator until it finishes, and this is the error you get. This is true since one of the actionStateChange is to remove an object from hashMap. How to do it safely, so when I delete something, it allows Iterator to know in advance so that I can avoid this error. Thank you in advance. If you need more information, I would be happy to satisfy your requests.
source share