IllegalStateException when removing an object with iterator

后端 未结 1 1169
野趣味
野趣味 2021-01-19 16:44

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 lTmpIndicsDesc = new          


        
1条回答
  •  旧巷少年郎
    2021-01-19 17:03

    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 itIndicsDesc = lTmpIndicsDesc.iterator(); itIndicsDesc.hasNext();) {
        String sTmpIndicsDesc = itIndicsDesc.next();
        for (Iterator itIndicsAvailableMark = lTmpIndicsAvailableMark.iterator(); itIndicsAvailableMark.hasNext();) {
            String sTmpIndicsAvailableMark = itIndicsAvailableMark.next();
            if (sTmpIndicsDesc.toUpperCase().equals(sTmpIndicsAvailableMark.toUpperCase())) {
                itIndicsDesc.remove();
                break; // added
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题