title: Why do we need to use iterator on ArrayList in Java?
body: <p>I was reading the answer mentioned to the question
  "<a href="https://stackoverflow.com/questions/15827703/do-we-ever-need-to-use-iterators-on-arraylist">Do we ever need to use Iterators on ArrayList?</a>".</p>
  
  <p>In the answer, the user stated something like this: "A big use case of iterators with ArrayLists is when you want to remove elements while iterating".</p>
  
  <p>This could be achieved even using remove method of ArrayList in Java. My question is why we need iterator in  ArrayList? </p>
  
  <p>Consider the code:  </p>
  
  <pre><code>import java.util.*;
  public class ocajp66 {
      public static void main(String[] args) {
          ArrayList a = new ArrayList();
          for (int i = 0; i &lt; 10; i++) {
              a.add(i);
          }
          System.out.printf("BEFORE ITERATOR\n");
          for (int i = 0; i &lt; a.size(); i++) {
              System.out.printf("I:%d\n", a.get(i));
          }
          System.out.printf("AFTER ITERATOR\n");
          Iterator i = a.iterator();
          while (i.hasNext()) {
              System.out.printf("I:%d\n", i.next());
          }
      }
  }
  </code></pre>
  
  <p>Can anybody explain the significance of the iterator? It would be wonderful if you could explain me with code.</p>