Java ArrayList迭代器删除崩溃

Java ArrayList迭代器删除崩溃,java,android,Java,Android,我在数组中有数组,我需要在第二个数组中找到一些项并删除父数组,但当我尝试删除数组时,我遇到了一个错误java.lang.IllegalStateException productsList = new ArrayList<>(mSortModels); for (ProductComponentsResponse component : filterData) { String componentId = component.getId();

我在数组中有数组,我需要在第二个数组中找到一些项并删除父数组,但当我尝试删除数组时,我遇到了一个错误java.lang.IllegalStateException

 productsList = new ArrayList<>(mSortModels);
    for (ProductComponentsResponse component : filterData) {
        String componentId = component.getId();
        int componentState = component.getState();
        Iterator<ProductResponse> iterator = productsList.iterator();
        while (iterator.hasNext()) {
            ProductResponse next = iterator.next();
            for (ProductComponentsResponse productComponentsResponse: next.getProductComponents()) {
                boolean containComponent = productComponentsResponse.getId().contains(componentId);
                if (componentState == ProductComponentsResponse.FilterState.NONE) {
                    continue;
                } else if (componentState == ProductComponentsResponse.FilterState.SELECTED) {
                    if (!containComponent) {
                        Log.d("component", String.valueOf(containComponent));
                        ***iterator.remove();*** - this error line
                    }
                } else if (componentState == ProductComponentsResponse.FilterState.UNSELECTED) {
                    if (containComponent) {
                        iterator.remove();

                    }
                }
            }
        }
    }
    notifyDataSetChanged();
productsList=新的ArrayList(mSortModels);
对于(ProductComponents响应组件:filterData){
字符串componentId=component.getId();
int componentState=component.getState();
迭代器迭代器=productsList.Iterator();
while(iterator.hasNext()){
ProductResponse next=iterator.next();
对于(ProductComponents响应ProductComponents响应:next.getProductComponents()){
布尔containComponent=productComponentsResponse.getId().contains(componentId);
if(componentState==ProductComponentResponse.FilterState.NONE){
继续;
}else if(componentState==ProductComponentResponse.FilterState.SELECTED){
如果(!containComponent){
Log.d(“组件”,String.valueOf(containComponent));
***迭代器.remove();***-此错误行
}
}else if(componentState==ProductComponentResponse.FilterState.UNSELECTED){
如果(包含组件){
iterator.remove();
}
}
}
}
}
notifyDataSetChanged();
迭代器.remove()
删除了父项,但您继续在子项上循环。有时可能会再次对同一父级调用
remove()
。那可能会导致你的车祸


要解决这个问题:放一个
断点迭代器.remove()
s之后执行code>以在删除其父循环时中断内部for循环。这样,您就不会继续循环已删除父项的子项。

我简化了您的代码片段,使其更易于理解:

ProductResponse next = iterator.next();
for (ProductComponentsResponse productComponentsResponse: next.getProductComponents()) {
    if (condition1) {
        continue;
    } else if (condition2) {
        iterator.remove();
    } else if (condition3) {
        iterator.remove();
    }
}
您正在调用
iterator.next()
一次,但随后进入
for
循环,如果
condition2
condition3
满意,则删除迭代器。然后继续循环,如果满足
condition2
condition3
,则再次删除在
for
循环的上一步中删除的相同迭代器。因此您得到了
非法状态异常
。您应该只执行一次
迭代器.remove()
调用,尝试放置一个
中断符在每个
块之后,否则如果

ProductResponse next = iterator.next();
for (ProductComponentsResponse productComponentsResponse: next.getProductComponents()) {
    if (condition1) {
        continue;
    } else if (condition2) {
        iterator.remove();
        break;
    } else if (condition3) {
        iterator.remove();
        break;
    }
}

你能更具体地描述一下这个错误吗?当时变量的状态是什么?您在哪一行得到错误。。将此信息添加到问题中,而不是添加到注释中,请复制