Java Concurrent Modification Exception Error

后端 未结 8 1799
庸人自扰
庸人自扰 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:17

    If you want to remove inside a loop you should use an iterator and its remove method

    public boolean removeStudent(String studentName)
    {
        Iterator itS = students.iterator();
        while(itS.hasNext())
        {
            Student student = itS.next();
            if (studentName.equalsIgnoreCasee(student.getName()))
            {
                itS.remove();
                return true;
            }
        }
        return false;
    }
    

提交回复
热议问题