Quote:
Originally Posted by slitz Like Hachima so nicely showed, a Iterator can not give you a index nor can it find a specific object at a certain index. The reason is simple.
Iterator is just a object that goes through a collection to give you a "next" object. It's supposed to work on all collections, even randomAccessCollections, Sets etc, where the order is irrelevant, it can't give you a index since that would make the method or function invalid for other type of collections. Foreach loop uses a iterator, so you're not supposed to get the index when using a for each. If you want the index, use a normal for loop, since that is your own implementation. |
I am filing this under "great interview questions" btw.