Exception in thread "main" java.util.ConcurrentModificationException
Squash the PC dirties the room Violet. The room's 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
それが私が受け取るエラーです。
私の方法は次のようになります
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();
}
}
}
}
つまり、私が理解しているのは、イテレータが終了する前にサイズを変更していることを意味し、これがエラーになります。これは、reactStateChangeの1つが、hashMapからオブジェクトを削除するためのものであるためです。これを安全に行うにはどうすればよいですか。何かを削除したときにIteratorに事前に通知して、このエラーを回避できるようにします。前もって感謝します。詳細が必要な場合は、ご要望にお応えさせていただきます。