import java.util.ArrayList; import java.util.Iterator; public class IteratorTest { public static void main(String[] args) { int trial = 3; ArrayList ali = new ArrayList<>(); for (int i=0; i<10; i++) { ali.add(i); } for (Integer ii : ali) { System.out.println("A" + ii); } Iterator alIt = ali.iterator(); while (alIt.hasNext()) { System.out.println("B" + alIt.next()); } for (Iterator alIt2 = ali.iterator(); alIt2.hasNext(); ) { System.out.println("C" + alIt2.next()); } int cc=0; if (trial==1) { for (Integer ii : ali) { System.out.println("R" + ii); if (cc%3==0) ali.remove(ii); cc++; } } if (trial==2) { cc=0; for (int i=0; i alIt2 = ali.iterator(); alIt2.hasNext(); ) { Integer ii = alIt2.next(); System.out.println("R" + ii); if (cc%3==0) alIt2.remove(); cc++; } } for (Iterator alIt2 = ali.iterator(); alIt2.hasNext(); ) { System.out.println("Z" + alIt2.next()); } } }