Using iterator() Method

To sequentially access elements of an array list, we use the iterator() method. We must import java.util.Iterator package to use this method. For example,


\begin{lstlisting}
import java.util.ArrayList;
import java.util.Iterator;
\par
c...
...m.out.print(iterate.next());
System.out.print('', '');
}
}
}
\end{lstlisting}

Output looks like ArrayList: Dog, Cat, Horse, Zebra,. hasNext() returns true if there is a next element in the array list. next() returns the next element in the array list.