I'm new to Java and is trying to learn the concept of iterator. I saw the code below from Java Tutorial Oracle, however, I am struggling to understand the function of this method and how it could be used. Could someone provide me with an example of how to use this method as part of an working code and explain it to me how it works?
public int indexOf(E e) {
for (ListIterator<E> it = listIterator(); it.hasNext(); )
if (e == null ? it.next() == null : e.equals(it.next()))
return it.previousIndex();
// Element not found
return -1;
}
This is a method for finding the index of an element e
(of generic type E
) that may (or may not) be contained by the underlying Collection
. If present, it uses it.previousIndex()
to return the index value for the element. Otherwise, it returns -1
.