Why iterator doesn't have any reset method?

前端 未结 5 1291
星月不相逢
星月不相逢 2020-12-29 19:52

Why? And what the best way to move iterator items pointer to the first position?

5条回答
  •  一向
    一向 (楼主)
    2020-12-29 20:46

    This is a general tendency adopted in JCF - keep the interface minimalistic , unless that makes some feature extremely difficult to work. This is the reason why you do not have separate interfaces for semantics like immutable collections, fixed-size collections ..

    As to why then a remove(Object) is provided ( as optional ) - Not providing this would make it impossible to safely remove an item from a collection while iterating over the collection - there is nothing that makes providing a reset() so compulsary.

    Again , why there is a separate ListIterator() ( providing methods like previous() and previousIndex() ) - With a List interface , the main functionality while it is being used is the ability to layout the elements wrt an index, and to be able to access them with an index-order , whether fixed or random order. This is not the case with other collections.Not providing this interface for a List will make it very difficult if not impossible to work smoothly with a list.

提交回复
热议问题