• 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

vector

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to add elements to the vector myElements. I had defined the vectors and its classes as shown below.

added UBB CODE tags to preserve indentation
[This message has been edited by Frank Carver (edited October 25, 2000).]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example seems very puzzling. I can't understand why you are trying to create all those Elements and Sets as member variables of every Element. If you just want to try out extending existing classes, consider the following version of your code:

I also have a few comments...
1. If you actually need a set, then I recommend looking at Sets as provided by the Collections API, in particular the Set interface and the HashSet class.
2. Your set implementation has an apparent "gotcha". Because you use Vector.contains() it will only reject duplicates if the actual object is the same, but not if you try to add a different object with the same name, which is probably what you want. The way round this is to provide an equals(Object) method in your Element class.
3. I have removed your "removeElement" as it was completely redundant - calling the one from Vector works fine.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have moved this to "Java In General (intermediate)", as it's not really about Utilities.
 
reply
    Bookmark Topic Watch Topic
  • New Topic