All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.pdx.cs410J.core.ModifyWhileIterating Maven / Gradle / Ivy

The newest version!
package edu.pdx.cs410J.core;

import java.util.*;

/**
 * This program demonstrates what happens when you modify a collection
 * while iterating over it.
 */
public class ModifyWhileIterating {

  public static void main(String[] args) {
    List list = new ArrayList<>();
    list.add("one");
    list.add("two");

    Iterator iter = list.iterator();
    while (iter.hasNext()) {
      String s = iter.next();
      if (s.equals("one")) {
        list.add(0, "start");
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy