File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "vector" Watch "vector" New topic


Madhu Jannu

Joined: Oct 24, 2000
Posts: 6
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

Joined: Jan 07, 1999
Posts: 6920
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.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
I have moved this to "Java In General (intermediate)", as it's not really about Utilities.
I agree. Here's the link:
subject: vector
It's not a secret anymore!