Questions tagged [deep-copy]

A deep copy of an object is a separate, fully independent duplicate of that object, such that any references / pointers it holds to other objects refer to deep copies of those to which the original's refer. Use this tag for questions regarding implementing or using deep copying methods.

Deep copying is a process by which an object is fully duplicated to create a copy that is wholly independent of the original. This differs from shallow copying with respect to the treatment of references / pointers to other objects: deep copying makes a deep copy of each referenced object, too, whereas shallow copying copies only the references. Deep copying is "deep" in the sense that it traverses the object's reference graph all the way to the bottom.

Related tags

Links

1483 questions
2611
votes
59 answers

Deep cloning objects

I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the original object. I don't often need this functionality,…
NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
2109
votes
40 answers

Copy array by value

When copying an array in JavaScript to another array: var arr1 = ['a','b','c']; var arr2 = arr1; arr2.push('d'); // Now, arr1 = ['a','b','c','d'] I realized that arr2 refers to the same array as arr1, rather than a new, independent array. How can…
Dan
  • 21,377
  • 5
  • 18
  • 16
753
votes
31 answers

What is the difference between a deep copy and a shallow copy?

What is the difference between a deep copy and a shallow copy?
David Locke
  • 17,926
  • 9
  • 33
  • 53
384
votes
26 answers

How do I clone a range of array elements to a new array?

I have an array X of 10 elements. I would like to create a new array containing all the elements from X that begin at index 3 and ends in index 7. Sure I can easily write a loop that will do it for me but I would like to keep my code as clean as…
user88637
  • 11,790
  • 9
  • 37
  • 36
308
votes
21 answers

How to clone ArrayList and also clone its contents?

How can I clone an ArrayList and also clone its items in Java? For example I have: ArrayList dogs = getDogs(); ArrayList clonedList = ....something to do with dogs.... And I would expect that objects in clonedList are not the same as in…
palig
  • 7,651
  • 6
  • 24
  • 18
286
votes
12 answers

What is the difference between shallow copy, deepcopy and normal assignment operation?

import copy a = "deepak" b = 1, 2, 3, 4 c = [1, 2, 3, 4] d = {1: 10, 2: 20, 3: 30} a1 = copy.copy(a) b1 = copy.copy(b) c1 = copy.copy(c) d1 = copy.copy(d) print("immutable - id(a)==id(a1)", id(a) == id(a1)) print("immutable - id(b)==id(b1)",…
deeshank
  • 4,286
  • 4
  • 26
  • 32
265
votes
10 answers

How to deep copy a list?

After E0_copy = list(E0), I guess E0_copy is a deep copy of E0 since id(E0) is not equal to id(E0_copy). Then I modify E0_copy in the loop, but why is E0 not the same after? E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for k in range(3): E0_copy =…
Shen
  • 2,911
  • 3
  • 15
  • 10
229
votes
13 answers

How do I copy a hash in Ruby?

I'll admit that I'm a bit of a ruby newbie (writing rake scripts, now). In most languages, copy constructors are easy to find. Half an hour of searching didn't find it in ruby. I want to create a copy of the hash so that I can modify it without…
Precipitous
  • 5,253
  • 4
  • 28
  • 34
143
votes
7 answers

Why and when to use angular.copy? (Deep Copy)

I've been saving all the data received from services direct to local variable, controller, or scope. What I suppose would be considered a shallow copy, is that correct? Example: DataService.callFunction() .then(function(response) { $scope.example…
Superman2971
  • 1,505
  • 2
  • 11
  • 10
122
votes
6 answers

Deep copying an NSArray

Is there any built-in function that allows me to deep copy an NSMutableArray? I looked around, some people say [aMutableArray copyWithZone:nil] works as deep copy. But I tried and it seems to be a shallow copy. Right now I am manually doing the copy…
ivanTheTerrible
  • 2,836
  • 4
  • 25
  • 25
94
votes
11 answers

How create a new deep copy (clone) of a List?

In the following piece of code, using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace clone_test_01 { public partial class MainForm : Form { public class Book { …
TheScholar
  • 2,527
  • 5
  • 23
  • 25
81
votes
8 answers

How to copy all items from one array into another?

How can I copy every element of an array (where the elements are objects), into another array, so that they are totally independent? I don't want changing an element in one array to affect the other.
hAlE
  • 1,283
  • 3
  • 13
  • 19
76
votes
12 answers

Kotlin data class copy method not deep copying all members

Could someone explain how exactly the copy method for Kotlin data classes work? It seems like for some members, a (deep) copy is not actually created and the references are still to the original. fun test() { val bar = Bar(0) val foo =…
triad
  • 20,407
  • 13
  • 45
  • 50
74
votes
2 answers

std vector C++ -- deep or shallow copy

I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards
kiriloff
  • 25,609
  • 37
  • 148
  • 229
71
votes
4 answers

Copying nested lists in Python

I want to copy a 2D list, so that if I modify one list, the other is not modified. For a one-dimensional list, I just do this: a = [1, 2] b = a[:] And now if I modify b, a is not modified. But this doesn't work for a two-dimensional list: a = [[1,…
SuperString
  • 21,593
  • 37
  • 91
  • 122
1
2 3
98 99