Remove objects from collection

pArrayList에서 iterating하면서 remove() 해야할 경우, Iterator를 사용함.

ArrayList list = new ArrayList(Arrays.asList(“a”,”b”,”c”,”d”));
for (int I = 0; i < list.size(); i++) {
list.remove(i); // 원소가 삭제될 때 list 사이즈가 줄면서 다른 원소들의 index도 바뀜
}

for (String s : list) {
list.remove(s); // ConcurrentModificationException 발생
}

Iterator it = list.iterator();
while (it.hasNext()) {
    String s = it.next(); // Iterator의 next()가 remove()보다 먼저 호출되어야 함
    it.remove();
}