In headfirst Java, example: attacking web games
The code provided in the book to check if the target was hit:
for (DotCom dotcomtotest:dotcomslist) {result = Dotcomtotest.checkyourself (userguess), if (Result.equals ("hit")) { break;} else if (Result.equals ("Kill")) {Dotcomslist.remove (dotcomtotest);
The target list is traversed in the same way as the iterator used, but an error occurs during execution.
Error type: Java.util.abstractlist$itr.next ()
Because the elements in the ArrayList are emptied, resulting in an error in the iterator
A variety of situations can be consulted
http://lggege.iteye.com/blog/249430
Code FIX:
for (int i = 0; i < dotcomslist.size (); i + +) {result = Dotcomslist.get (i). Checkyourself (userguess); if (Result.equals ("H It ")) {break;} else if (Result.equals ("Kill")) {dotcomslist.remove (i); break;}}
Java ArrayList errors caused by iterators when emptying elements