• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding a an element to a vector

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The add method of vector class returns false if you attempt to add an element with a duplicate value.
But i believe vectors allow duplicate values. Am i missing something here?
- Thanks
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector provides the add() method because it implements the List interface. Now, the point to understand here is, the behaviour of this method is dependent on the class that implements this interface (here, Vector). As List (which is acutally a Collection) is an interface, the contract that it defines for add() method is quite loose. Different implementations may ( and should ) provide different behaviour. For. eg. the add() method of a class implementing Set should return false if you try to add a duplicate element.
But Vector being a List, can support duplicate elements and so it always returns true.
HTH,
Paul.
http://pages.about.com/jqplus
------------------
Get Certified, Guaranteed!
 
Paul A
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the statement that you wrote "The add method of vector class returns false if you attempt to add an element with a duplicate value." is not correct. It always returns true.
It is correct for a class implementing Set though.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic