I am having trouble understanding this error. Error message: Exception in thread "main" java.util.ConcurrentModificationException
public static int lift(List<String> list) {
int index = 0;
for (String element : list) {
list.remove(index);
list.add(index, element.toUpperCase());
index++;
}
I'm trying to replace list items but I guess removing and adding in the same iteration is considered a concurrent modification? Is that right? I tried element = element.toUppercase() but that wasn't actually modifying the list at all...
Please help! I am a beginner (obviously)
list.replaceAll(String::toUpperCase);.list.removeline, and then uselist.setinstead oflist.add. That would work without ConcurrentModificationException; but it is not efficient for all list types (e.g. LinkedList would be inefficient).for (ListIterator<String> it = list.listIterator(); it.hasNext();) { it.set(it.next().toUpperCase()); }