I am trying to remove an object with a certain attribute from a list of Objects, I have red other questions about this problem but I guess I am missing something.
this method doesn't work, because I guess you can not remove elements from a list while iterating through it, so I created a Dog removeDog = null;
variable, which is set to the right dog during the loop, and after
it finished to iterate I can just do animals.remove(removeDog);
(I don't know if this is the best solution)
for (Dog dog : animals.getDogList()){
if (dog.getDogName().equals(dogName)){
animals.getDogList().remove(dog);
}
}
This method instead works perfectly, in my eyes it should have the same problem of the previous one, but in this case Java let me remove one or more elements during iteration, but I don't understand the difference.
for (Dog dog : animals.getDogList()){
if (dog.getWeigh() > weight){
animals.getDogList().remove(dog);
}
}