Next() Method of an ArrayList's Iterator

public Iterator <E> iterator () {
      return new Iterator () {
            private int i = 0;
            public E next () {
                  return elements [i];
            }
            boolean hasNext () {
                  return i < size ;
            }
       };
 }

Regarding the implementation of the ArrayList’s Iterator in Subsection 8.9.3, mentioned below, should the field i have been incremented by next()? i.e.:

public Iterator <E> iterator () {
      return new Iterator () {
            private int i = 0;
            public E next () {
                  E currentElement = elements[i];
                  i++;
                  return currentElement;
            }
            // ...
       };
 }

Absolutely. Thanks!

1 Like