Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java and pointers

Started by Smoo Oct 14, 2000 at 10:54 PM 6 replies 700+ views
Original Post
Smoo
Smoo
Greets folks, I''ve got a problem. Now I know that java is pointers but I''m at a loss here. I have a class called mapfile that contains all the info I need for a map. I tried using a hashtable so I would have htMapFile.put(key, mapfile); then I would change the contents of mapfile and repeat the above process... I was hoping that it wouldn''t do what it did. htMapFile key 1 --> points to mapfile change mapfile contents htMapFile key 2 --> changed mapfile but htMapFile key 1 --> also points to changed mapfile hence the joy of pointers is there a way to perhaps clone mapfile so when it dumps the info into the hashtable i can have in key 1 different data than key 2? There has to be a way to work around it... somehow Help/Suggestions would be appreciated Smoo
Jim_Ross
Jim_Ross
Another solution to get a deep copy would be to serialze the object, then unserialize it into another variable.
c_wraith
c_wraith
Hmm... Yes, in your case, the clone() method, even if it is supported, isn't what you are looking for. I, too, thought that using serialization would be a clever solution, but it would copy too deeply... It would copy every object stored in the map, not just the mapping information.

I'd say that the anonymous poster's approach is on the right track, although more complicated than necessary. You should be able to copy a Hashtable object by just using something like this:

Hashtable htMapFileCopy = new Hashtable(htMapFile);

Of course, since I went to the work of figuring out how to do deep copying using serialization, I'm going to include that code anyway... Just remember that it IS NOT what you want, because it'll copy everything you store in it, key and value pairs, and the entire graph of objects each of those elements references. If your design is interconnected enough, you could end up duplicating your entire world with this...


                

import java.io.*;

public final class DeepCopy {

//don't allow this class to be instantiated

private DeepCopy() {}

public static Object copy(Serializable s)
throws IOException, ClassNotFoundException
{

//serialize the object into a byte array

ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(s);
oo.flush();
byte [] b = bo.toByteArray();
oo.close();

//construct a copy of the object from the byte array

ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream oi = new ObjectInputStream(bi);
Object copied = oi.readObject();
oi.close();

//return the copy

return copied;

}//copy


}// class DeepCopy



(Edited because the source tag ate my white space...)

Edited by - c_wraith on October 16, 2000 2:31:02 AM
Jerry Lynn
Jerry Lynn
You could still use object serialization, just create custom readObject and writeObject methods. You can then control exactly what gets persisted and what doesn''t.

,Jerry
Smoo
Smoo
c_wraith, what you wrote was interesting and I''ll try out a few things with that.

You bring up a couple of questions,
1. The mapfile class contains only a few variables and a TriggerObject[] which holds info for events. This is all for 1 map only. The hashtable is there for caching this stuff basically.
1. Load the map in mapfile
2. Dump it to hashtable
3. If goes to a different map repeat 1 and 2
4. If goes to a map that has been loaded into hashtable dump from hashtable to mapfile

and mapfile = null; between loadings so it would lose all pointer info to other vars, etc.. so serialization should work.. shouldn''t it?

However when you bring up the point of
Hashtable htMapFileCopy = new Hashtable(htMapFile);

I don''t see how for instance if I have
1. Map1 is dumped to htMapFile key 1
2. Create a new instance (Yes this would work for it here)
3. Map2 is dumped into htMapFile key 2
4. But since it''s pointers, key 1 and key 2 are now alike
so I don''t follow how creating a new instance of hitMapFile into htMapFileCopy would do the trick here because it will have removed all info from htMapFileCopy and replace it with htMapFile.

You have given me something to try out and I''m gonna try it out right now.

I just thought of something else I could try since the
Hashtable htMapFileCopy = new Hashtable(htMapFile);
gave me an idea.

if I have this:
Hashtable htMapFileCopy = new Hashtable(htMapFile);
Hashtable htMapFile = new Hashtable(htMapFileCopy);

nothing in htMapFile should be linked to mapfile and I could re-input new data into mapfile and dump it to htMapFile.

Thanks folks, I''ll keep you posted
Smoo
Smoo
Smoo
Well I think I''m stupid.

all it took was a simple
mapfile = null;
mapfile = new MapFile();
reset all stats in mapfile then dump it into the hashtable.

before I i had just put all stats in mapfile to null and thought that would work. well, creating a new instance of the mapfile loses all pointer info, nullifying the vars doesn''t.

c_wraith you did help out quite a bit actually by giving me the idea of a new instance. It wasn''t something that I didn''t know about, just I didn''t think to use. Well it shows I''m still a newbie

Anyways, now that that''s working hopefully my attempts at caching will work.

Smoo

Btw the Hashtable htCopy = new Hashtable(oldHt);
doesn''t work. it''s not one of the constructors.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.