• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why iterator dont have add() method?

 
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?
 
Marshal
Posts: 64638
225
  • 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.
 
Rancher
Posts: 43009
76
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!