• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why iterator dont have add() method?  RSS feed

 
jayaa mani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Collections has Iterator and List .

But Iterator has remove() function but do not have add().

List has both�


Can anyone please tell me the technical reason why add() is not implemented in Iterator?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a technical reason.
An Iterator is designed for going through a Collection and seeing what members it contains. Not for adding new members. That is why Iterators on ly have 3 methods.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's more, with remove it's clear which element is referred to, while add could do a number of different things: insert before the current element, insert after the current element, insert at the end... One would also need to define whether the newly added element will then also be part of the Iterator. That would amount to a number of complications for the basically simple task of iterating through the collection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!