javaでiteratorを利用するサンプルコード
Javaコード:
public class cftIteratorTest {
Collection<Integer> collection = new ArrayList<>();
@Before
public void setcftUp() {
Random random = new Random();
for (int i = 0; i < 21; ++i) {
collection.add(random.nextInt(i + 1));
}
}
@Test
public void client() {
System.out.print(“before iterator:");
for (Iterator<Integer> iterator = collection.iterator(); iterator.hasNext(); ) {
Integer integer = iterator.next();
System.out.printf(" %d", integer);
if (integer == 0) {
//collection.remove(i);
iterator.remove();
}
}
System.out.printf(“%n after iterator:");
for (Integer integer : collection) {
System.out.printf(" %d", integer);
}
}
}