why there is no add method in Iterator interface

前端 未结 6 1602
逝去的感伤
逝去的感伤 2021-02-01 06:05

In Iterator Sun added the remove method to remove the last accessed element of the collection. Why there is no add method to add a new element to the collection? Wh

6条回答
  •  [愿得一人]
    2021-02-01 06:47

    I cannot think of any theoretical reason why add() could not have been included in Iterator. Just as the Iterator can allow for elements being removed from the collection through itself, it could be designed to handle elements being added in the same way.

    But I will say that in all my years of programming in Java -- over 15! -- I have never wanted an Iterator.add() method. So I suspect it's simply not all that useful.

提交回复
热议问题