프로그래밍/안드로이드
[자바/Android] java.util.ConcurrentModificationException
초보플밍지기
2012. 12. 27. 10:26
java.util.ConcurrentModificationException
ConcurrentModificationException 는 대부분 iterator를 사용하는는 동안에 해당 iterator에 추가 삭제등을 시도하면 ConcurrentModificationException 이 발생합니다.
iterator 가 부모의 내용이 변경되지 않는 것을 전제로 하기 때문인데, 이 때 Iterator 로 빼낼 객체의 clone 을 만들어서 사용하면 해결됩니다.
Iterator it = ((ArrayList<String>)list.clone()).iterator();
while(it.hasNext()) {
it.remove();
}
위의 내용은 일반적으로 알려진 내용이구요. 근데 가끔씩 다른 상황에서 나오는 경우가 발생하는데 바로 ArrayList를 출력할때도 발생합니다.
ArrayList<String> testArrList = new ArrayList<String>();
testArrList.add("test1");
testArrList.add("test2");
testArrList.add("test3");
Log.e("test", "testArrList 출력 : "+testArrList);
위와 같이 출력할때 testArrList가 변경되어도 ConcurrentModificationException 이 발생한답니다.
자주 발생되는건 아니구요.... 참고만 하셔요.~