I have a doubt with the next()
method on iterators. If I have as a part of my code this lines (with arrayOfStrings
size = 4):
Iterator<String> it = arrayOfStrings.iterator(); //arrayOfString is ArrayList<String>
while(it.hasNext()) {
String e = it.next();
System.out.println(e);
}
At the very first iteration, the iterator starts pointing to element with index 0? or like the "index -1" ?
I ask because as far as I know the next()
method returns the next element in the collection.
So, if at the very first iteration the iterator starts at index 0 when next()
is invoked, it returns the element at index 1 and I won´t be able to do nothing with the element at index 0?
The code you show will correctly print all elements of the
ArrayList
. Theit.next()
call will, at its first call, also the first element. This element is usually denoted with index 0.Note that you might want to rename
arrayOfStrings
as technically it's no array, it's more aList
. A user might due the name think that it is an array.Documentation
In my opinion you are right that the documentation of the method can be a bit confusing since it is so compact:
However the description probably needs to be so undetailed since
Iterator
is a very frequently used interface, not only for collections. I could imagine a class where the first iteration element should not be the first element of the underlying structure. For example some kind of stream that was already read until a given point. So basically it is up to the implementing class to decide what the first element is. But for collections it will truly be the first element (index 0).For-each
Also note that the iteration you show is equivalent to how the extended for-loop (for-each) works in Java. So a syntax like
will be treated like
In fact this is the reason why you can use everything that implements
Iterable
in extended for loops.Knowing this it would rather be confusing if
iter.next()
would skip the first element. I mean by the name for-each is supposed to iterate all elements.Insights
Let's take a closer look at how the method is implemented. Therefore we will first see what
ArrayList#iterator
does. Take a look at its source code:The
Itr
class is a private class ofArrayList
(source code). And here is theItr#next
method:So what it essentially returns is the element at index
i
which is where the cursor was at the point of calling the method. It also already advances the cursor by one for the next call of the method.The cursor however is implicitly initialized with
0
due to:So the first call of
next
will indeed return the element at index0
which is the first element.My way to imagine the way iterators work is to think about it as the thing that is placed between the indexes. So when the first
next()
is being called you can return the value stored under the [0] index and after that call, iterator waits between index [0] and [1] for the next call to return for you value stored at index [1]. After the secondnext()
call iterator waits between [1] and [2] and so on...It is just my way to get used to iterators but it might be helpful for you some way.
Think of
next
as a two-step process. First it gets the next item in the iterator, then it increments the pointer to point to the next item. So, when you create a new iterator, it is initialized to return the first item (index 0) in your list.