Question:
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
sharmi
2009-03-15 22:57:26 UTC
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
I know:
The first one performs a deep copy of the array, the second one is shallow.
but what does that mean?
Please explain with meaning/examples
Four answers:
Blackcompe
2009-03-15 23:26:12 UTC
a shallow copy is a copy of the reference and a deep copy is a copy of the structure. Object.clone() will return a deep copy, whereas Arrays.copyOf() will return a shallow copy



int[] a = {1, 2, 3, 4};

int[] b = new int[4];

b = Arrays.copyOf(a, 0, a.length);



on the heap



a

\

{1, 2, 3, 4}

/

b

///////////////////////////



int[] a = {1, 2, 3, 4};

int[] b = new int[4];

b = (int[])a.clone();



on the heap



a ->{1, 2, 3, 4}

b ->{1, 2, 3, 4}



////////////////////////



The memory consumption has doubled.
fisch
2016-11-30 06:38:31 UTC
international Wrestling leisure, Inc. (WWE) is a publicly traded, privately controlled integrated media (focusing in television, internet, and stay events), and activities leisure employer dealing in maximum cases interior the professional wrestling marketplace, with substantial gross sales components additionally coming from movie, music, product licensing, and direct gross sales. Vince McMahon is the final public proprietor and Chairman of the employer and his spouse Linda McMahon holds the situation of chief government Officer (CEO). mutually with their toddlers, government vice chairman of world Media Shane McMahon and government vice chairman of skills and imaginitive Writing Stephanie McMahon-Levesque, the McMahons carry approximately 70% of WWE's financial pastime and ninety six% of all vote casting capability interior the employer. WTF (what the ****) what the heck, what the hell, what in God's call (on the internet) WTF can stand additionally for international commerce Federation, yet is many times used for What The ****
Lie Ryan
2009-03-15 23:55:51 UTC
A deep copy means the array is copied recursively, its inner arrays and objects are copied as well.



A shallow copy means only the outermost array is copied. The objects in a shallow copy of an array is the same objects as the original array. If you mutate the objects, the change will be reflected to the other copy.



On contrary, the objects of a deep copy is a copy of the objects from the original array. If you mutate the objects, the change doesn't affect the copy since they are both completely separate.



Giving example doesn't make sense.
leo
2009-03-15 23:13:42 UTC
shallow copy means for eg



object o = new object();

object a;



a = o;

a contains reference of only o not complete object

this is shallow copy

simply saying, shallow copy copies the structure of the object, deep copy copies structure as well as data.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...