Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何删除列表中的项目?_List_Classcastexception_Concurrentmodification_Copyonwritearraylist - Fatal编程技术网

List 如何删除列表中的项目?

List 如何删除列表中的项目?,list,classcastexception,concurrentmodification,copyonwritearraylist,List,Classcastexception,Concurrentmodification,Copyonwritearraylist,我在nest for循环中有两个列表,当我在内部匹配一个项目时,我想删除它,这样性能就会提高 List<String[]> brandList = readCsvFile("/tmp/brand.csv"); List<String[]> themeList = readCsvFile("/tmp/theme.csv"); for (String[] brand : brandList) { for (String[] theme : themeList) {

我在nest for循环中有两个列表,当我在内部匹配一个项目时,我想删除它,这样性能就会提高

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);
        }
    }
}
现在我该怎么办?省略删除?还是别的

我想这就是我需要的:

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 brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
for(字符串[]品牌:品牌列表){
List toRemove=new ArrayList();
for(字符串[]主题:主题列表){
if(品牌[0]。等于(主题[0])){
toRemove.add(主题);
}
}
for(字符串[]主题:toRemove){
themeList.removeAll(主题);
}
}

在对
集合进行迭代时,不能从该集合中删除项,Java中的
foreach
循环基本上就是这样做的。您必须创建一个新的
列表
,收集所有要删除的元素,然后在遍历
集合
后批量删除它们:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
List<String[]> toRemove = new ArrayList<String[]>();

for (String[] brand : brandList) {
    for (String[] theme : themeList) {
        if (brand[0].equals(theme[0])) {
            toRemove.add(theme);
        }
    }
}
themeList.removeAll(theme);
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
List toRemove=new ArrayList();
for(字符串[]品牌:品牌列表){
for(字符串[]主题:主题列表){
if(品牌[0]。等于(主题[0])){
toRemove.add(主题);
}
}
}
themeList.removeAll(主题);

在对
集合进行迭代时,不能从该集合中删除项,Java中的
foreach
循环基本上就是这样做的。您必须创建一个新的
列表
,收集所有要删除的元素,然后在遍历
集合
后批量删除它们:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");
List<String[]> toRemove = new ArrayList<String[]>();

for (String[] brand : brandList) {
    for (String[] theme : themeList) {
        if (brand[0].equals(theme[0])) {
            toRemove.add(theme);
        }
    }
}
themeList.removeAll(theme);
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
List toRemove=new ArrayList();
for(字符串[]品牌:品牌列表){
for(字符串[]主题:主题列表){
if(品牌[0]。等于(主题[0])){
toRemove.add(主题);
}
}
}
themeList.removeAll(主题);

虽然没有那么漂亮,但您可以使用迭代器:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");

for (String[] brand : brandList) {
    Iterator<String[]> themeIterator = themeList.iterator();
    while (themeIterator.hasNext()) {
        String[] theme = themeIterator.next();
        if (brand[0].equals(theme[0])) {
            themeIterator.remove();
            // If you are sure there is only one theme per brand, add a break here
            // break;
        }
    }
}
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
for(字符串[]品牌:品牌列表){
迭代器themeIterator=themeList.Iterator();
while(themeIterator.hasNext()){
String[]theme=themeIterator.next();
if(品牌[0]。等于(主题[0])){
删除该运算符。删除();
//如果您确定每个品牌只有一个主题,请在此添加休息
//中断;
}
}
}

取决于列表的具体类型(数组列表、链表等),它可能比复制变量快,也可能比复制变量快。

它没有那么漂亮,但您可以使用迭代器:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");

for (String[] brand : brandList) {
    Iterator<String[]> themeIterator = themeList.iterator();
    while (themeIterator.hasNext()) {
        String[] theme = themeIterator.next();
        if (brand[0].equals(theme[0])) {
            themeIterator.remove();
            // If you are sure there is only one theme per brand, add a break here
            // break;
        }
    }
}
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
for(字符串[]品牌:品牌列表){
迭代器themeIterator=themeList.Iterator();
while(themeIterator.hasNext()){
String[]theme=themeIterator.next();
if(品牌[0]。等于(主题[0])){
删除该运算符。删除();
//如果您确定每个品牌只有一个主题,请在此添加休息
//中断;
}
}
}

取决于
列表的具体类型
主题列表
是(数组列表、链表等),它可能比复制变量快,也可能比复制变量快。

如果您使用的是Java 8功能,类似这样的功能可能有用,也可能更快:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");

// Extract unique values of the first column from the brand list
// into a structure suited for fast lookup
Set<String> names = brandList.stream()
        .map(columns -> columns[0])
        .collect(Collectors.toSet())

// Remove all entries from themeList where the value of the
// first column exists in names
themeList.removeIf(columns -> names.contains(columns[0]))
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
//从品牌列表中提取第一列的唯一值
//转换为适合快速查找的结构
Set name=brandList.stream()
.map(列->列[0])
.collect(收集器.toSet())
//删除列表中的所有条目,其中
//第一列存在于名称中
themeList.removeIf(列->名称.contains(列[0]))

如果您使用的是Java 8功能,类似这样的功能可能会起作用,而且可能会更快:

List<String[]> brandList = readCsvFile("/tmp/brand.csv");
List<String[]> themeList = readCsvFile("/tmp/theme.csv");

// Extract unique values of the first column from the brand list
// into a structure suited for fast lookup
Set<String> names = brandList.stream()
        .map(columns -> columns[0])
        .collect(Collectors.toSet())

// Remove all entries from themeList where the value of the
// first column exists in names
themeList.removeIf(columns -> names.contains(columns[0]))
List brandList=readCsvFile(“/tmp/brand.csv”);
List themeList=readCsvFile(“/tmp/theme.csv”);
//从品牌列表中提取第一列的唯一值
//转换为适合快速查找的结构
Set name=brandList.stream()
.map(列->列[0])
.collect(收集器.toSet())
//删除列表中的所有条目,其中
//第一列存在于名称中
themeList.removeIf(列->名称.contains(列[0]))

你的代码不是我想要的,我可以把它们放在列表中吗;内部-外部循环?将其置于循环外部更有效,因为只对要删除的所有元素执行一次删除操作。这当然是可能的,但是从这个代码片段中我看不出语义上的区别在哪里。你的代码不是我想要的,我可以把它们放在列表中吗;内部-外部循环?将其置于循环外部更有效,因为只对要删除的所有元素执行一次删除操作。这当然是可能的,但从这个代码片段中我看不出语义上的区别在哪里。一个品牌有多个主题!这就是中断被注释掉的原因:)此代码也将生成一个
ConcurrentModificationException
。另外,
foreach
循环在内部只不过是一个
迭代器
。使用迭代器的要点是
Iterator.remove()
方法允许在迭代过程中删除项(与
collection.remove(x)
相反)。具有某些限制,例如当调用
remove()
时,列表上必须只有一个迭代器在运行。有关详细信息,请参阅所用混凝土集合的文档。正如您所知,尽管它使用了迭代器,
foreach
语法没有公开此方法。一个品牌有多个主题!这就是中断被注释掉的原因:)此代码将生成一个
ConcurrentModificationExce