Hi all...

i am trying to add a vector to the hashtable as a value.This vector contains three fields.But the problem is while adding new vector to the hashtable as value the old vector is also replaced with new one....
So please help to solve this problem..

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

How are you calling the hashtable function?

That's what a Hashtable (you should prefer HashMap over it and ArrayList over Vector, but that's another story) is all about...
Or are you trying to add the content of the Vector to the content of the Vector that's already there?
That's easy enough to accomplish, using code like


I have the feeling that what he is doing, is adding the Vector as a Vector, then clearing the Vector, assigning new values, and adding it to the Hashtable again.

@OP: If this is what you are doing then of course the values are changing for "both" Vectors, because there is only one of them. When you add something to a Hashtable, you are not adding a copy of it to the hashtable, but rather are adding a copy of the reference to the object (in this case a Vector) to the Hashtable. If you then turn around and change the object that was referenced (in this case the Vector again), then you are changing the content for the object referenced in the Hashtable as well, since it is the same object.

You need to create a new Vector, rather than editing the Vector that you have already added.

yes, that's what I'm thinking too. He's calling put with the first Vector, creates a new Vector and calls put with that one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.