【发布时间】:2016-02-13 13:04:21
【问题描述】:
我在嵌套循环中有两个列表,当我在内部匹配一个项目时,我想将其删除以提高性能。
List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
for (String[] brand : brandList) {
for (String[] theme : themeList) {
if (brand[0].equals(theme[0])) {
themeList.remove(theme);
}
}
}
我收到java.util.ConcurrentModificationException 错误。如果我改成 CopyOnWriteArrayList,报错如下:
CopyOnWriteArrayList<String[]> themeList = (CopyOnWriteArrayList<String[]>)readCsvFile("/tmp/theme.csv");
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.concurrent.CopyOnWriteArrayList
现在我该怎么办?省略删除?还是别的什么?
我认为这是我需要的:
List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
for (String[] brand : brandList) {
List<String[]> toRemove = new ArrayList<String[]>();
for (String[] theme : themeList) {
if (brand[0].equals(theme[0])) {
toRemove.add(theme);
}
}
for (String[] theme : toRemove) {
themeList.removeAll(theme);
}
}
【问题讨论】:
标签: list classcastexception concurrentmodification copyonwritearraylist