Why does Scanner implement Iterator?

前端 未结 4 1896
余生分开走
余生分开走 2021-01-04 08:48

I was just wondering why java.util.Scanner implements java.util.Iterator?

Scanner implements the remove method and throws an UnsupportedOperationExcept

4条回答
  •  梦毁少年i
    2021-01-04 09:38

    Because implementing iterator allows the scanner to be used wherever a read only iterator can be used.

    Furthermore it does implement the contract. From the Iterator documentation (emphasis mine):

    remove() Removes from the underlying collection the last element returned by this iterator (optional operation).

提交回复
热议问题