java - example - why to use iterator instead of for loop

Why do we need to use iterator on ArrayList in Java? (4)

Q: Why do we need an iterator in ArrayList?

We don't - just as you've shown in your code, you can iterate and do core operations on an ArrayList without an iterator. But it's a nice to have feature.

Q: Can anybody explain the significance of the iterator?

In addition to its design value, one I could see is its fail-fast feature. I quote this paragraph from the ArrayList documentation:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

You were looking for code, you can actually see the ArrayList's iterator implementation here:

This question already has an answer here:

I was reading the answer mentioned to the question "Do we ever need to use Iterators on ArrayList?".

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".

This could be achieved even using remove method of ArrayList in Java. My question is why we need iterator in ArrayList?

Consider the code:

import java.util.*;
public class ocajp66 {
    public static void main(String[] args) {
        ArrayList a = new ArrayList();
        for (int i = 0; i < 10; i++) {
        System.out.printf("BEFORE ITERATOR\n");
        for (int i = 0; i < 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()) {

Can anybody explain the significance of the iterator? It would be wonderful if you could explain me with code.

As you have stated iterator is used when you want to remove stuff whilst you iterate over the array contents. If you don't use an iterator but simply have a for loop and inside it use the remove method you will get exceptions because the contents of the array changes while you iterate through. e.g: you might think array size is 10 at the start of the for loop but it wont be the case once you remove stuff.. so when u reach the last loops probably there will be IndexOutofBoundsException etc.

It is clear that an ArrayList-like API could work without the iterator() method. However, an ArrayList is a Collection and the iterator() method is defined in the Collection interface ... so ArrayList has to implement it.

The point about deleting from an ArrayList is that doing it by indexing requires some thought:

    for (int i = 0; 
         i < a.size(); // Hoist this at your peril
         i++) {
        if (a.get(i) == something) {
            i--;  // Leave this out at your peril

And it gets worse if you need to remove the list element in a method called from the loop ... 'cos the method has to then say that it has removed an element so that the caller can adjust the loop index.

A third reason why iterator is a good thing on an ArrayList is that it allows you to use Java 5's for (type var : iterable) ... syntax.

The bottom line is that you don't have to use iterators on ArrayList instances. If you don't want to, then don't.

This is an example of how it is possible to get the results you want in several different ways. This kind of redundancy is not unique to Java.

  • for (int i=0; i < myArray.length; i++) { ... }

This syntax was introduced in the very early versions of Java. It iterates over a usual Java array in a for { } loop. This is generally safe because Java arrays are fixed length and so "Index Out of Bounds" exceptions are not possible.

  • for (int i=0; i < myArrayList.size(); i++ { ... }

This syntax reflects a later release of Java, after the introduction of the Collections API which introduced ArrayList. Classes that implement the Collection interface, as already mentioned above, must implement an Iterator but you don't have to use it. This for { } loop doesn't, but the danger here is that ArrayLists are not fixed size. If it should shrink in the body of your for loop, and exception can result.

  • for (MyArrayType t : myArrayList) { }

This syntax was also released in a later release of Java. It is called the enhanced for loop. Any collection class that provides an Iterator by implementing the Iterable interface can take advantage of this syntax. This allows iterating over items in a collection without having to explicitly instantiate an Iterator. A favorite way to use this in a JavaFX Application is to loop through a bunch of controls to set a property to a value, eg. to reset the contents of a group of TextFields:

for (TextField tf : new TextField[] { txtf1, txtf2, txtf3, txtfa, txtfb, txtfc}) {
  • while (myCollectionIterator.hasNext()) { }

You can always explicitly instantiate an Iterator. This is safe to use when collection size is changing (from the Collection's own methods). It correct to say that the Iterator is more closely a property of the Iterable interface than a feature of the core Java language. But you can still use it as a language-like feature (in the enhanced for loop) thanks to later Java releases.

These constructs provide redundancy, but they are not identical. There are nuances of each that enable one to be particularly useful at a given time. You should use all of them.