Re: Hashtable values seems not probably retrieved...
From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 07/14/04
- Next message: Alan Meyer: "Re: how to detect OS in java"
- Previous message: Newbie Programmer: "Re: Hashtable values seems not probably retrieved..."
- In reply to: Newbie Programmer: "Re: Hashtable values seems not probably retrieved..."
- Next in thread: Eric Sosman: "Re: Hashtable values seems not probably retrieved..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 14 Jul 2004 16:23:30 GMT
Newbie Programmer <Unavailable@sorry.com> scribbled the following:
> "Murray" <parps@SPAMOFFoptusnet.SPAMMAGE.com.au> wrote in message
> news:40f54a38$0$18194$afc38c87@news.optusnet.com.au...
>> > Yes, I did write the two records to the hashtable. I have included
>> > a method called t41.writeFile() which consists of:
>> >
>> > void writeFile() {
>> > try {
>> > FileOutputStream fos = new FileOutputStream(MY_HASH);
>> > ObjectOutputStream oos = new ObjectOutputStream(fos);
>> > oos.writeObject(hashtable_ptr);
>> > oos.close();
>> > } catch (Exception e) {
>> > e.printStackTrace();
>> > }
>> > }
>> >
>> > This should write records to the filesystem, right?
>> > Any clue? Thanks again.
>>
>> I think you misunderstood Ryan's point. The reason you're getting the same
>> object for both keys is because you're STORING the same object for both
>> keys.
>>
>> tmp_vector.clear();
>> tmp_vector.add("vector_1a");
>> tmp_vector.add("vector_1b");
>> t41.writeData("key_1", "string_1", tmp_vector);
>>
>> // write 2nd record to hashtable
>> tmp_vector.clear();
>> tmp_vector.add("vector_2a");
>> tmp_vector.add("vector_2b");
>> t41.writeData("key_2", "string_2", tmp_vector);
>>
>> This code is adding the same Vector object to the map each time. Java
>> doesn't make a copy of the object at the time you add it to the map.
> Rather
>> it just stores a reference to the object. The "value" in the both entries
> of
>> your map will still be pointing at the same object that tmp_vector is
>> pointing at, and any change you make to tmp_vector will be reflected in
> both
>> entries of the map (because they are referencing the same object!).
>>
>> If you want to store two distinct and independent vectors in your map,
>> instead of simply clear()ing tmp_vector after you add the first record,
> you
>> must instead create a new Vector object.
>>
> Thanks. I was expecting the clear() will clear the vector and give
> me another brand new one, because the API said:
> clear()
> Removes all of the elements from this Vector.
> Isn't it?
Where do you think it says it gives you a new Vector? You get the same
old Vector back, only this time without any elements.
In fact, there is **NO WAY** that a method call, through an object
reference, can change that reference to another object. You have to
use assignments.
-- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby." - Kent Brockman
- Next message: Alan Meyer: "Re: how to detect OS in java"
- Previous message: Newbie Programmer: "Re: Hashtable values seems not probably retrieved..."
- In reply to: Newbie Programmer: "Re: Hashtable values seems not probably retrieved..."
- Next in thread: Eric Sosman: "Re: Hashtable values seems not probably retrieved..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|