deep-copy

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

こ雲淡風輕ζ 提交于 2019-11-26 06:33:45
问题 Question: Is there a way to use existing objective-c methods to do a full deep copy of a NSDictionary or NSArray, that themselves have nested dictionaries or arrays within them? That is I have read the problem may be when it hits a nested dictionary or array it only copies the pointer to the nested item, and not copy the item truely. Background: So as an example for me I\'m trying to load/save the following config with NSUserDefaults and when loading need to convert the immutable copies one

Deep copy of List<T>

南楼画角 提交于 2019-11-26 06:02:44
问题 I\'m trying to make a deep copy of a generic list, and am wondering if there is any other way then creating the copying method and actually copying over each member one at a time. I have a class that looks somewhat like this: public class Data { private string comment; public string Comment { get { return comment; } set { comment = value; } } private List<double> traceData; public List<double> TraceData { get { return traceData; } set { traceData = value; } } } And I have a list of the above

How to make a deep copy of Java ArrayList [duplicate]

删除回忆录丶 提交于 2019-11-26 05:27:01
问题 Possible Duplicate: How to clone ArrayList and also clone its contents? trying to make a copy of an ArrayList. The underlying object is simple containing on Strings, ints, BigDecimals, Dates and DateTime object. How can I ensure that the modifications made to new ArrayList are not reflected in the old ArrayList? Person morts = new Person(\"whateva\"); List<Person> oldList = new ArrayList<Person>(); oldList.add(morts); oldList.get(0).setName(\"Mortimer\"); List<Person> newList = new ArrayList

How to create a deep copy of an object in Ruby?

寵の児 提交于 2019-11-26 03:38:59
问题 I did some searching found some different methods and posts about creating a deep copy operator. Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or hashes. Working in Ruby 1.9.2. 回答1: Deep copy isn't built into vanilla Ruby, but you can hack it by marshalling and unmarshalling the object: Marshal.load(Marshal.dump(@object)) This isn't perfect though, and won't work for all objects. A more robust method: class Object def deep_clone return @deep

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

我们两清 提交于 2019-11-26 03:38:26
问题 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 { public string title = \"\"; public Book(string title) { this.title = title; } } public MainForm() { InitializeComponent(); List<Book> books_1 = new List<Book>(); books_1.Add( new Book(\"One\") ); books_1.Add( new Book(\"Two\") ); books_1.Add( new Book(\"Three\") ); books_1.Add( new

How to deep copy a list?

风格不统一 提交于 2019-11-26 01:23:05
问题 I have some problem with a List copy: So After I got E0 from \'get_edge\' , I make a copy of E0 by calling \'E0_copy = list(E0)\' . Here I guess E0_copy is a deep copy of E0 , and I pass E0_copy into \'karger(E)\' . But in the main function. Why does the result of \'print E0[1:10]\' before the for loop is not the same with that after the for loop? Below is my code: def get_graph(): f=open(\'kargerMinCut.txt\') G={} for line in f: ints = [int(x) for x in line.split()] G[ints[0]]=ints[1:len

Copying nested lists in Python

自闭症网瘾萝莉.ら 提交于 2019-11-26 00:25:54
问题 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, 2],[3, 4]] b = a[:] If I modify b , a gets modified as well. How do I fix this? 回答1: For a more general solution that works regardless of the number of dimensions, use copy.deepcopy() : import copy b = copy.deepcopy(a) 回答2: b = [x[:] for x in a]

How do I do a deep copy of a 2d array in Java?

随声附和 提交于 2019-11-25 23:58:48
问题 I just got bit by using .clone() on my 2d boolean array, thinking that this was a deep copy. How can I perform a deep copy of my boolean[][] array? Should I loop through it and do a series of System.arraycopy \'s? 回答1: Yes, you should iterate over 2D boolean array in order to deep copy it. Also look at java.util.Arrays#copyOf methods if you are on Java 6. I would suggest the next code for Java 6: public static boolean[][] deepCopy(boolean[][] original) { if (original == null) { return null; }

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

て烟熏妆下的殇ゞ 提交于 2019-11-25 22:57:21
问题 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)\", id(b) == id(b1)) print(\"mutable - id(c)==id(c1)\", id(c) == id(c1)) print(\"mutable - id(d)==id(d1)\", id(d) == id(d1)) I get the following results: immutable - id(a)==id(a1) True immutable - id(b)==id(b1) True mutable - id(c)==id(c1) False mutable

How to clone ArrayList and also clone its contents?

核能气质少年 提交于 2019-11-25 21:45:36
问题 How can I clone an ArrayList and also clone its items in Java? For example I have: ArrayList<Dog> dogs = getDogs(); ArrayList<Dog> clonedList = ....something to do with dogs.... And I would expect that objects in clonedList are not the same as in dogs list. 回答1: You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go. public static List<Dog> cloneList(List<Dog> list) { List<Dog> clone = new ArrayList<Dog>(list.size()); for (Dog item