Jump to content

View more

View more

Image of the Day

#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

[java] Java and pointers

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Smoo   Members   


Posted 14 October 2000 - 04:54 PM

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

#2 Anonymous Poster_Anonymous Poster_*   Guests   


Posted 14 October 2000 - 06:51 PM

you want to clone an object. I think there is a method in Object called clone(). Since all classes are subclasses of object every class has the clone() method. I assume it works like this:

Map m1 = new Map();
Map m2 = m1.clone();

So now m1 and m2 are two different but identical objects.

#3 Anonymous Poster_Anonymous Poster_*   Guests   


Posted 15 October 2000 - 09:19 AM

The clone method is protected.
To use it your class needs to implement Cloneable.
Also clone makes a shallow copy, i.e. it doesn''t clone any internal objects.
So if you have Map m2=m1.clone(); and say there is an object in Map called picture, then m1.picture==m2.picture. Thats is, they point to the same thing, modifying one will mdify the other also.

A better solution to your problem is to just make two maps using the same constructor.

#4 Jim_Ross   Members   


Posted 15 October 2000 - 06:31 PM

Another solution to get a deep copy would be to serialze the object, then unserialize it into another variable.

#5 c_wraith   Members   


Posted 15 October 2000 - 07:25 PM

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);
byte [] b = bo.toByteArray();

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

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

//return the copy

return copied;


}// class DeepCopy

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

Edited by - c_wraith on October 16, 2000 2:31:02 AM

#6 Jerry Lynn   Members   


Posted 16 October 2000 - 03:14 AM

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.


#7 Smoo   Members   


Posted 16 October 2000 - 05:19 AM

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

#8 Smoo   Members   


Posted 16 October 2000 - 05:36 AM

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.


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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.