ConcurrentModificationException for ArrayList
2022-08-31 11:27:07
我有以下代码:
private String toString(List<DrugStrength> aDrugStrengthList) {
StringBuilder str = new StringBuilder();
for (DrugStrength aDrugStrength : aDrugStrengthList) {
if (!aDrugStrength.isValidDrugDescription()) {
aDrugStrengthList.remove(aDrugStrength);
}
}
str.append(aDrugStrengthList);
if (str.indexOf("]") != -1) {
str.insert(str.lastIndexOf("]"), "\n " );
}
return str.toString();
}
当我尝试运行它时,我得到,任何人都可以解释为什么它会发生,即使代码在同一线程中运行?我该如何避免呢?ConcurrentModificationException