clone

How do you make a deep copy of an object in Java?

跟風遠走 提交于 2019-11-25 22:23:49
问题 In java it\'s a bit difficult to implement a deep object copy function. What steps you take to ensure the original object and the cloned one share no reference? 回答1: A safe way is to serialize the object, then deserialize. This ensures everything is a brand new reference. Here's an article about how to do this efficiently. Caveats: It's possible for classes to override serialization such that new instances are not created, e.g. for singletons. Also this of course doesn't work if your classes

What is the most efficient way to deep clone an object in JavaScript?

前提是你 提交于 2019-11-25 22:10:10
问题 What is the most efficient way to clone a JavaScript object? I\'ve seen obj = eval(uneval(o)); being used, but that\'s non-standard and only supported by Firefox. I\'ve done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. I\'ve also seen recursive copying functions with various flaws. I\'m surprised no canonical solution exists. 回答1: Native deep cloning It's called "structured cloning", works experimentally in Node 11 and later, and hopefully will land in

Java: recommended solution for deep cloning/copying an instance

爱⌒轻易说出口 提交于 2019-11-25 21:58:57
问题 I\'m wondering if there is a recommended way of doing deep clone/copy of instance in java. I have 3 solutions in mind, but I can have miss some, and I\'d like to have your opinion edit: include Bohzo propositon and refine question: it\'s more about deep cloning than shallow cloning. Do it yourself: code the clone by hand properties after properties and check that mutable instances are cloned too. pro: - control of what will be performed - quick execution cons: - tedious to write and maintain

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

How do I copy an object in Java?

安稳与你 提交于 2019-11-25 21:40:22
问题 Consider the code below: DummyBean dum = new DummyBean(); dum.setDummy(\"foo\"); System.out.println(dum.getDummy()); // prints \'foo\' DummyBean dumtwo = dum; System.out.println(dumtwo.getDummy()); // prints \'foo\' dum.setDummy(\"bar\"); System.out.println(dumtwo.getDummy()); // prints \'bar\' but it should print \'foo\' So, I want to copy the dum to dumtwo and change dum without affecting the dumtwo . But the code above is not doing that. When I change something in dum , the same change is

How do you do a deep copy of an object in .NET (C# specifically)? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-25 21:39:25
问题 This question already has answers here : Deep cloning objects (46 answers) Closed 5 years ago . I want a true deep copy. In Java, this was easy, but how do you do it in C#? 回答1: I've seen a few different approaches to this, but I use a generic utility method as such: public static T DeepClone<T>(T obj) { using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; return (T) formatter.Deserialize(ms); } } Notes: Your class MUST be

Deep cloning objects

淺唱寂寞╮ 提交于 2019-11-25 21:34:20
问题 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, so when it\'s been necessary, I\'ve resorted to creating a new object and then copying each property individually, but it always leaves me with the feeling that there is a better or more elegant way of handling the situation. How can I clone or