• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

how to deep copy a linkedlist?

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any existing method to deep copy a linkedlist?
Thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no existing method that I'm aware of. Assuming the list elements implement Cloneable though, it's pretty easy to iterate through the list and clone() each element, putting each cloned element in a new LinkedList. Alternately, if the elements implement Serializeable then you can serialize the whole list, and then deserialize it - this generates a whole new structure. This is a very flexible approach - much slower in general than implementing custom methods for each structure you want to deep-copy, but once you implement it, the same code can clone just about any structure composed of Serializeable components. JavaWorld has a nice article about this - check it out. In general I'd recommend the first method as being much more efficient - but remember that the clone() method will just make a shallow copy, so if your list elements contain references to other objects, it may well be simpler to use the serialization alternative. Good luck.
[This message has been edited by Jim Yingst (edited May 09, 2001).]
 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jim,
It means that I should overload the clone () to do deep copy. is it right?
Simon
 
Author
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using serialization if you need to make a deep copy.
------------------
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you want to overload clone() - perhaps you meant override? You can do that - of course, you'd have to subclass LinkedList, which may or may not be worth the trouble. You could also just create a new method:
<code><pre> static LinkedList deepCopy(LinkedList original) { ... }</pre></code>
It's up to you.
The problem is, what sort of objects are inside the LinkedList? You if you want deep copies of them, then you'll have to provide new methods for making a deep copy of each class involved. Or, as I (and John) said, use serialization.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, the subject of making deep copies is discussed in depth by the latest Sun JDC Tech Tips. Highly recommended to everyone reading this thread.
- Peter
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!