When trying to add/remove some stuff from/in a list in a loop java could raise ConcurrentModificationException
exceptions. After searching in google I came across ways of overcoming the issue such as applying the interface iterator and when you want to remove an item you need to remove it from the list through the iterator itself using the remove()
function.
However, there isn't an add()
function among the iterator's methods so the only way to add an element with no exception raised would be to use the loop for (int i = 0; i < list.size(); i++)
by using the index/position. However, this loop is extremely inefficient with large lists as you need to get the index position of the item in question by traversing all the way through the list. So my question is do you know any other way except the way I mentioned which you can use to add an item to a list?
Thanks