中級 › ConcurrentModificationException の原因と直し方 › パターン3
ConcurrentModificationException の原因と直し方
ループ中のリスト変更で発生します。
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class Main { 5 public static void main(String[] args) { 6 List<String> list = _____________________________; ^ 7 list.add("x"); 8 System.out.println(list); 9 } 10 }
Exception in thread "main" java.lang.UnsupportedOperationExceptionexited with code 1
空欄を埋めてビルドを通す
List.ofは変更不可のリストを返します。追加や削除をするならArrayListに包み直します。
次の問題