Hi,
In my application I need to deep copy an Object (which really is an array of
Objects that contains more arrays and so on - and which is not always the
same structure - but the elements are either String, Integer or Boolean).
How best to do this? At the moment I save the object to a file and read it
back in again - but this is not the most efficient method of doing it I
guess.
What other way can I do it? Can I instead of saving it to a file just store
it in memory? Or should I illiterate through the object (arrays) and copy
them element by element?
Thanks for any ideas,
Alan.
The code I have is below:
//vdata is the Object that i need to deep copy
//write
FileOutputStream out = new FileOutputStream("temp.dat");
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(vdata);
oos.flush();
oos.close();
out.close();
//read
FileInputStream in = new FileInputStream("temp.dat");
ObjectInputStream ois = new ObjectInputStream(in);
Object vdataDup = ois.readObject();
ois.close();
in.close();
Roedy Green - 09 Apr 2004 19:26 GMT
>Or should I illiterate through the object (arrays) and copy
>them element by element?
that is the traditional way. The problem is for a deep copy you need
to know the structure. Presumably you could write a generic deep copy
that used Reflection, but it would be quite slow.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.