Is it possible to have multiple iterators in a single collection and have each keep track independently? This is assuming no deletes or inserts after the iterators were assigned.
Basically, you can't use two Iterators on the same Vector simultaneously. Not for Vector, ArrayList, LinkedList, or other common collections.
Iterators are used to traverse through the Java collections. There are three types of iterators.
An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping. To use an Iterator, you must import it from the java.util package.
Yes.
Sometimes it's really annoying that answers have to be 30 characters.
Yes, it is possible. That's one reason they are iterators, and not simply methods of the collection.
For example List
iterators (defined in AbstractList
) hold an int
to the current index (for the iterator). If you create multiple iterators and call next()
a different number of times, each of them will have its int cursor
with a different value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With