001package edu.pdx.cs410J.core; 002 003import java.util.*; 004 005/** 006 * This program demonstrates what happens when you modify a collection 007 * while iterating over it. 008 */ 009public class ModifyWhileIterating { 010 011 public static void main(String[] args) { 012 List<String> list = new ArrayList<>(); 013 list.add("one"); 014 list.add("two"); 015 016 Iterator<String> iter = list.iterator(); 017 while (iter.hasNext()) { 018 String s = iter.next(); 019 if (s.equals("one")) { 020 list.add(0, "start"); 021 } 022 } 023 } 024 025}