Java Concurrent Modification Exception Error

后端 未结 8 1798
庸人自扰
庸人自扰 2020-12-11 03:50

Im playing around with some code for my college course and changed a method from

public boolean removeStudent(String studentName)
{
    int index = 0;
    f         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 04:13

    You shouldn't delete objects from a collection while using a for-each statement - this will cause exceptions as your iterator faces a changed collection in the course of its iterations. (the for loop) either use a regular for loop (for int i = 0; i < 100; i++) etc... or keep the objects to remove in a list, and remove them outside of the for loop.

    Also, you remove the object by index where index is : 0 , 1 , 2 but index should actaully be the index of the student.

提交回复
热议问题