4

I've been strugling with this bug since a while and I don't know where the problem is. My code is like this :

ArrayList<String> lTmpIndicsDesc = new ArrayList<String>(indicsDesc);
ArrayList<String> lTmpIndicsAvailableMark = new ArrayList<String>(indicsAvailableMark);
    for (Iterator<String> itIndicsDesc = lTmpIndicsDesc.iterator(); itIndicsDesc.hasNext();) {
        String sTmpIndicsDesc = itIndicsDesc.next();
        for (Iterator<String> itIndicsAvailableMark = lTmpIndicsAvailableMark.iterator(); itIndicsAvailableMark.hasNext();) {
            String sTmpIndicsAvailableMark = itIndicsAvailableMark.next();
            if (sTmpIndicsDesc.toUpperCase().equals(sTmpIndicsAvailableMark.toUpperCase())) {
                itIndicsDesc.remove();
            }
        }
    }

It raise an IllegalStateException on the remove call.

I've been wondering if the problem could appear because I was removing the last item of my list but it seems to bug even in the middle of the process.

Can you guys give me an explanation please ?

1 Answer 1

4

You are removing an element from the lTmpIndicsDesc List from inside the inner loop. This means your inner loop might try to remove the same element twice, which would explain the exception you got. You should break from the inner loop after removing the element:

for (Iterator<String> itIndicsDesc = lTmpIndicsDesc.iterator(); itIndicsDesc.hasNext();) {
    String sTmpIndicsDesc = itIndicsDesc.next();
    for (Iterator<String> itIndicsAvailableMark = lTmpIndicsAvailableMark.iterator(); itIndicsAvailableMark.hasNext();) {
        String sTmpIndicsAvailableMark = itIndicsAvailableMark.next();
        if (sTmpIndicsDesc.toUpperCase().equals(sTmpIndicsAvailableMark.toUpperCase())) {
            itIndicsDesc.remove();
            break; // added
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I did this but it mess my parsing actually, I forgot to mention it sorry, do you think my parsing methods are the ones in fault ? The break doesn't change anything except prevent the IllegalStateException right ?
@SimonMILHAU The break breaks out of the internal loop. Assuming you posted all the logic of your inner loop, it just contains a condition, and calls remove if that condition is true. Once you call remove, there's nothing else to do in the inner loop, so break makes perfect sense. If your actual code has additional logic in the inner loop, you'll need to use some boolean variable (to determine whether to call remove or not) instead of break.
Thank you @Eran, it's crystal clear now, I think the problem come from a bad parsing condition or something like that, but now I can test it without an exception !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.