拡張 for 文(for (String x : list))でコレクションを回している最中に、そのコレクション自体を変更(追加・削除)すると出ます。
出るコード
List<String> names = new ArrayList<>(List.of("Aoi", "Sota", "Yui"));
for (String name : names) {
if (name.equals("Sota")) {
names.remove(name); // 反復中に list を直接変更している
}
}
エラーメッセージ
Exception in thread "main" java.util.ConcurrentModificationException
直し方
Iterator の remove() を使うか、removeIf でまとめて削除します。
names.removeIf(name -> name.equals("Sota"));