lec21 - 21 Copying collections and depth of copy Binary...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 21 Copying collections and depth of copy Binary Search Tree ADT A Linked Implementation of Binary Search Tree Binary Search Tree Operation Time Costs CSE 12 Binary Search Trees Copying Collections However sometimes it is useful to create a copy of a Collection, as another Collection of the same type Java provides various mechanisms to do this But first doing it requires understanding the difference between different depths of copy... How deep is the copy? What does it mean to copy a data structure? You might create a copy of a pointer pointing to an existing data structure object (very shallow copy) You might create a new data structure object, containing copies of the existing data structure's instance variables (shallow copy) You might also create copies of objects the instance variables point to (deep copy) You might also create copies of objects those objects' instance variables point to (deeper copy)... etc., etc. shallow deep Very shallow copy If you just copy the pointer to the data structure object, you just have created another way of referring to one and the same object next data next data next data next data next data size x 15 10 40 77 x y x header 4 So for example now x.clear() or y.clear() do the same thing Shallow copy If you create a new data structure object and copy its instance variables you are still sharing structure next data next data next data next data next data x 15 10 40 77 x y x size header 4 This can lead to problems: for example x.clear() now puts the object y points to in an inconsistent state! size header 4 Deep copy Create a new data structure object, and also new data structure elements, but share data item objects x y x size header 4 Note: If the data item objects are mutable, changing their values will change them in both data structures size header 4 next data next data next data next data next data 15 10 40 77 next data next data next data next data next data Deeper copy Create a new data structure object, and also new data structure elements, and also copy data item objects x y x size header 4 Now you have two completely independent copies (but what if the data objects have pointers to other objects?...) size header 4 next data next data next data next data next data 15 10 40 77 next data next data next data next data next data 15 10 40 77 Copying objects in Java If you want to design instances of a class so that they are copyable, how can you do that? There are several options 1. Define a copy constructor for your class: a constructor that takes as argument a pointer to an existing instance of the class, and initializes the new instance to be a copy of the existing one 2. Define a static factory method in the class that takes as argument a pointer to an existing instance of the class, creates a new instance, and initializes the new instance to be a copy of the existing one 3. Use the Cloneable interface and override the clone() instance method Binary Search Trees...
View Full Document

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 29

lec21 - 21 Copying collections and depth of copy Binary...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online