Rectangle 27 0

java Concurrent Modification Error for Object ArrayList?


for(Iterator<Exercise> iterator = z.iterator(); iterator.hasNext();)
{
    Exercise c = iterator.next();
    if(!compareSkillLevel(c,a)|| !compareEquipment(c)|| !compareFocus(c)
            || !compareTraining(c))
        iterator.remove();
}
for(int i = 0;i < z.size(); i++)
{
    Exercise c = z.get(i);
    if(!compareSkillLevel(c,a)|| !compareEquipment(c)|| !compareFocus(c)
            || !compareTraining(c))
        z.remove(i--)
}

As other answers have pointed out, the problem you have here is that you are trying to remove from a list while you iterate over it. This isn't allowed in java, except if you remove the element using the iterator's remove method. This means you have two ways about fixing this code.

Note the i-- here, which is necessary because the act of removing an element from the ArrayList shifts the existing elements over, meaning the next element you want to examine is in the place of the last element.

or, you can loop over the array list using its index, and instead do something like

Note
Rectangle 27 0

java Concurrent Modification Error for Object ArrayList?


for(Iterator<Exercise> iterator = z.iterator(); iterator.hasNext();)
{
    Exercise c = iterator.next();
    if(!compareSkillLevel(c,a)|| !compareEquipment(c)|| !compareFocus(c)
            || !compareTraining(c))
        iterator.remove();
}
for(int i = 0;i < z.size(); i++)
{
    Exercise c = z.get(i);
    if(!compareSkillLevel(c,a)|| !compareEquipment(c)|| !compareFocus(c)
            || !compareTraining(c))
        z.remove(i--)
}

As other answers have pointed out, the problem you have here is that you are trying to remove from a list while you iterate over it. This isn't allowed in java, except if you remove the element using the iterator's remove method. This means you have two ways about fixing this code.

Note the i-- here, which is necessary because the act of removing an element from the ArrayList shifts the existing elements over, meaning the next element you want to examine is in the place of the last element.

or, you can loop over the array list using its index, and instead do something like

Note