The remove method is used in the For each loop ., Eachremove
List <String> list = new ArrayList <String> (); list. add ("boss"); list. add ("good"); list. add ("No"); list. add ("Fine"); System. out. println (list); // [boss, good, No, Fine] Collections. sort (list); System. out. println (list); // [Fine, No, boss, good] list. add ("we"); list. add ("you are"); list. add ("he calls"); list. add ("see you tomorrow"); int I = 0; while (true) {for (String dwonCountCmd: list) {if (I <30) {System. out. println (dwonCountCmd + I); I ++;} else {// removes an element of the Set in the loop. If you do not have a break at this time, the list will be returned with an error. remove (dwonCountCmd); System. out. println (list. toString (); break ;}}}
This error is reported.
Exception in thread "main" java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(Unknown Source) at java.util.AbstractList$Itr.next(Unknown Source) at Test2.main(Test2.java:47)