0

I have array inside array, I a need find in second array some items and remove parent array, but when I'm trying remove array I had an error 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();
Qwerty
  • 17
  • 5
  • 3
    can you be more specific of the error? what is the state of your variables at that time? at which line do you get the error? .. – Stultuske Sep 29 '17 at 07:15
  • add this information to the question, not in a comment, please – Stultuske Sep 29 '17 at 07:18
  • Possible duplicate of [Removing object from ArrayList in Java](https://stackoverflow.com/questions/14813330/removing-object-from-arraylist-in-java) – Dima Kozhevin Sep 29 '17 at 07:26

2 Answers2

1

Iterator.remove() removed the parent, but you continue looping over the children. Sometimes maybe calling remove() on the same parent again. That could cause your crash.

To solve this: put a break; after both iterator.remove()s to break out of the inner for loop when you have removed its parent. This way you won't continue looping the children of a removed parent.

Frank
  • 12,010
  • 8
  • 61
  • 78
0

I have simplified your code snippet to make it more easy to follow:

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

You are calling iterator.next() once but then you get in a for loop where if condition2 or condition3 is satisfied you remove the iterator. Then you continue looping and if condition2 or condition3 is satisfied you again remove the same iterator which was removed in a previous step of the for loop. So you get the IllegalStateException. You should do the iterator.remove() call only once, try placing a break; after each else if block.

ProductResponse next = iterator.next();
for (ProductComponentsResponse productComponentsResponse: next.getProductComponents()) {
    if (condition1) {
        continue;
    } else if (condition2) {
        iterator.remove();
        break;
    } else if (condition3) {
        iterator.remove();
        break;
    }
}
pleft
  • 7,567
  • 2
  • 21
  • 45