• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

vector

 
Madhu Jannu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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).]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have moved this to "Java In General (intermediate)", as it's not really about Utilities.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic