Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

deep copy an Object

Thread view: 
alan jeeves - 09 Apr 2004 18:35 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.