JAVA 의 집합 과 교체 기 요소 삭제
5844 단어 제로 베이스
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
:
: ,
: , remove ,
c.remove(); :java.util.ConcurrentModificationException
, remove , it2.remove();
*/
public class CollectionTest05 {
public static void main(String[] args) {
Collection c = new ArrayList();
/*
:
: ,
*/
Iterator it = c.iterator();
// , ,
// next :
//java.util.ConcurrentModificationException
c.add("abcd");
c.add(100);
Iterator it2 = c.iterator();
//
while(it2.hasNext()){
Object o = it2.next();
//
// c.remove();
/*
remove , , ,
java.util.ConcurrentModificationException
*/
// remove
it2.remove(); //
/* 。。 :
,
remove ,
, , 。
remove , 。
*/
System.out.println(o);
}
System.out.println(c.size()); //0
}
}