java.util.ConcurrentModificationException problem

前端 未结 2 580
醉酒成梦
醉酒成梦 2021-02-19 01:22

On this code I get an java.util.ConcurrentModificationException the method is in a webservice and first reads the file and checks if the vakNaam is in the file. Then it will be

2条回答
  •  名媛妹妹
    2021-02-19 02:00

    The error is in this part:

    for (String s : tempFile){
        String [] splitted = s.split(" ");
        if (splitted[0].equals(naam)){
            tempFile.remove(s);
            found = true;   
        }
    } 
    

    Don't modify the list you are iterating over. You could solve this by using the Iterator explicitely:

    for (Iterator it = tempFile.iterator(); it.hasNext();) {
        String s = it.next();
        String [] splitted = s.split(" ");
        if (splitted[0].equals(naam)){
            it.remove();
            found = true;   
        }
    } 
    

提交回复
热议问题