코드를 수정을 하는데 'ConcurrentModificationException' 이란 에러가 발생했다.

왜 발생했는지도 모르고 왜지? 하고보니, list를 잘 못 수정했을시에 발생하는 에러였다.


만약 나와 같이 이러한 에러가 난다면 아래처럼 코드를 확인 바란다.


1. for문과 list를 같이 사용하는 부분을 확인.

(1. Check the part that uses for statement and list together.)


for(String str : list){}

for(i=0; i< list.size(); i++){}


2. 해당 for문에서 아래와 같은 구문이 있는지 찾기.

(2.Make sure that the for statement contains the following syntax)

 - list.remove();

 - list.add();


결론적으로 따지면

for(String str : list){

    if(str.equals("1"){

       list.remove(str); || list.add("2");

    }

}


즉 이와같은 구문을 사용을 했을 것이다.

(나는 add(); 를 추가하여 발생했다..ㅜㅜ)


이와 같이 list의 중간에 추가를 하거나 삭제를 발생하면 오류가 난다.

이렇게 오류가 나는 이유는 불러온 원본 데이터가 변경이 되어 발생하는 것으로 list에 직접적인 조작이 일어나서 라고한다.

(The reason for this error is that the original data that was loaded is changed and that list is manipulated directly.)


그럼 이를 수정을 한다고 하면

(Modify this way.)


이와같은 형태로 형식에 맞게 수정을 하면된다고 합니다.

for (Iterator<Character> str = list.iterator(); str.hasNext(); ) {

  Character letter = str.next();

  if (Character.isDigit(letter)) {

    str.remove();

  }

}

이와같은 형태로 형식에 맞게 수정을 하면된다고 합니다.



+ Recent posts