clone

java.util.Date clone or copy to not expose internal reference

亡梦爱人 提交于 2019-11-26 15:31:15
问题 It is best practice not to expose the internal references of an Object (Entity). So if an Object has a field of type java.util.Date then for example the getter for this field should return not the original date but an copy of it. But for an java.util.Date there are two common ways to create that copy: clone: (Date) originalDate.clone() copy via constructor new Date(originalDate.getTime()) My question is, which way is better, and why? 回答1: If it's definitely just a Date , it won't make any

Copying a HashMap in Java

拟墨画扇 提交于 2019-11-26 15:25:13
问题 I am trying to keep a temporary container of a class that contains member : HashMap<Integer,myObject> myobjectHashMap A class called myobjectsList Then I do myojbectsListA = new myojbectsList(); myojbectsListB = new myobjectsList(); then: Add some hashmap items to A (like2) then myobjectListB = myobjectListA; //B has 2 then: Add hashmap items to A; (like 4 more) then return A to the items stored in B; myobjectListA = myobjectListb; but when I do this B grows with A while I am adding hashmap

What&#39;s the difference between Bitmap.Clone() and new Bitmap(Bitmap)?

让人想犯罪 __ 提交于 2019-11-26 15:20:30
As far as I can tell, there are two ways of copying a bitmap. Bitmap.Clone() Bitmap A = new Bitmap("somefile.png"); Bitmap B = (Bitmap)A.Clone(); new Bitmap() Bitmap A = new Bitmap("somefile.png"); Bitmap B = new Bitmap(A); How do these approaches differ? I'm particularly interested in the difference in terms of memory and threading. It is the common difference between a "deep" and a "shallow" copy, also an issue with the almost-deprecated IClonable interface. The Clone() method creates a new Bitmap object but the pixel data is shared with the original bitmap object. The Bitmap(Image)

Does calling clone() on an array also clone its contents?

笑着哭i 提交于 2019-11-26 15:16:16
If I invoke clone() method on array of Objects of type A, how will it clone its elements? Will the copy be referencing to the same objects? Or will it call (element of type A).clone() for each of them? Bozho clone() creates a shallow copy. Which means the elements will not be cloned. (What if they didn't implement Cloneable ?) You may want to use Arrays.copyOf(..) for copying arrays instead of clone() (though cloning is fine for arrays, unlike for anything else) If you want deep cloning, check this answer A little example to illustrate the shallowness of clone() even if the elements are

Any way to clone HTML5 canvas element with its content?

好久不见. 提交于 2019-11-26 14:33:05
Is there any way to create a deep copy of a canvas element with all drawn content? Actually the correct way to copy the canvas data is to pass the old canvas to the new blank canvas. Try this function. function cloneCanvas(oldCanvas) { //create a new canvas var newCanvas = document.createElement('canvas'); var context = newCanvas.getContext('2d'); //set dimensions newCanvas.width = oldCanvas.width; newCanvas.height = oldCanvas.height; //apply the old canvas to the new one context.drawImage(oldCanvas, 0, 0); //return the new canvas return newCanvas; } Using getImageData is for pixel data access

How do I create a copy of an object in PHP?

一个人想着一个人 提交于 2019-11-26 14:30:49
It appears that in PHP objects are passed by reference. Even assignment operators do not appear to be creating a copy of the Object. Here's a simple, contrived proof: <?php class A { public $b; } function set_b($obj) { $obj->b = "after"; } $a = new A(); $a->b = "before"; $c = $a; //i would especially expect this to create a copy. set_b($a); print $a->b; //i would expect this to show 'before' print $c->b; //i would ESPECIALLY expect this to show 'before' ?> In both print cases I am getting 'after' So, how do I pass $a to set_b() by value, not by reference? In PHP 5+ objects are passed by

Git commands not working in Mac terminal: “dyld: Symbol not found: ___strlcpy_chk” error

拥有回忆 提交于 2019-11-26 13:57:00
问题 I am using the command git clone ssh://.... and getting the following error on the terminal: dyld: lazy symbol binding failed: Symbol not found: ___strlcpy_chk Referenced from: /usr/local/git/bin/git Expected in: /usr/lib/libSystem.B.dylib dyld: Symbol not found: ___strlcpy_chk Referenced from: /usr/local/git/bin/git Expected in: /usr/lib/libSystem.B.dylib Trace/BPT trap: 5 回答1: I needed to install command line tools from Xcode. To do so- Open Xcode and hit Cmd + , Click Downloads Install

Cloning JPA entity

时光毁灭记忆、已成空白 提交于 2019-11-26 13:01:29
问题 I have a JPA entity already persisted in the database. I would like to have a copy of it (with a different id), with some fields modified. What is the easiest way to do this? Like: setting it\'s @Id field to null and persisting it will work? will I have to create a clone method for the entity (copying all fields except the @Id )? is there any other approach (like using a cloning framework)? 回答1: Use EntityManager.detach . It makes the bean no longer linked to the EntityManager. Then set the

jQuery Clone table row

白昼怎懂夜的黑 提交于 2019-11-26 12:41:55
问题 I have a table with an Add button on the end. When you click this button I want a new table row to be created underneath the current one. I also want the input fields on this row to be blank. I am trying to do this using .clone() but it clones all the rows on the page. Please help. Thanks Script $(\"input.tr_clone_add\") .live(\'click\', function(){ $(this).closest(\'.tr_clone\') .clone() .insertAfter(\".tr_clone\") }); HTML <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0

How to make a copy of an object in C# [duplicate]

*爱你&永不变心* 提交于 2019-11-26 12:40:12
问题 This question already has answers here : Deep cloning objects (46 answers) Closed 6 years ago . Let\'s say that I have a class: class obj { int a; int b; } and then I have this code: obj myobj = new obj(){ a=1, b=2} obj myobj2 = myobj; Now the above code makes a reference to the first obj. What I want is that myobj2 refers to a copy of the myobj with changes not being reflected in the original. I have searched SO and the solutions thus far seems complicated. Is there an easier way to do this.