clone

Why is the Object class's clone() method giving a deep copy of object?

喜夏-厌秋 提交于 2019-12-04 04:45:46
问题 As per the JAVA documentation, the super.clone() when called returns a shallow copy of the object. In the code below I have two objects name and id ; and one primitive variable num . When the super.clone() method is called on the first object, it seems to be creating a deep copy of the objects(name and id) in addition to an expected copy of only num. After cloning the object obj, I have changed its name and id fields. These changes should be reflected in the cloned object if a shallow copy

Copy instance variable of type java.util.Random to create object in same state

…衆ロ難τιáo~ 提交于 2019-12-04 04:29:32
问题 I'm implementing a simulated annealing (SA) algorithm, where I need to copy states (e. g. to remember best solution so far). I implemented a copy method, since it's discouraged to use java's clone() . SA is a heuristic algorithm, so the next step to take is determined randomly. This is done by using a Random object, which I want to copy too. Although it's not requiered by the algorithm, I want the copy to have exactly the same state. But this is only the case, if I make a 'copy' direct after

Why are Java enums not clonable?

二次信任 提交于 2019-12-04 04:11:59
It's too late to change the question, but more precise would have been to ask "Why does clone() not allow singletons?". A copy() method would be more convenient. Is there any reason why enums in Java cannot be cloned? The manual states that This guarantees that enums are never cloned, which is necessary to preserve their "singleton" status. But returning the instance itself would also preserve its status, and I would be able to handle associated enums the same way as other clonable objects. One may argue that The general intent [of clone()] is that, for any object x, the expression: x.clone()

JQuery adding class to cloned element

梦想的初衷 提交于 2019-12-04 03:54:31
This is my script: $('.addprop').click(function() { $('#clone').clone().insertAfter('.addprop'); }) I need to add a class to the new element that is being created. Is it possible? Yes, it is: $('.addprop').click(function() { $('#clone').clone().addClass('newClass').insertAfter('.addprop'); }) Although you're cloning an element based on its id , $('#clone') , so note that there will be two elements sharing the same id , which makes the result invalid HTML, so I'd suggest: $('.addprop').click(function() { $('#clone').clone().attr('id',id += 1).addClass('newClass').insertAfter('.addprop'); });

Cloning an array with its content

做~自己de王妃 提交于 2019-12-04 03:24:04
I want to make a copy of an array, to modify the copy in-place, without affecting the original one. This code fails a = [ '462664', '669722', '297288', '796928', '584497', '357431' ] b = a.clone b.object_id == a.object_id # => false a[1][2] = 'X' a[1] #66X722 b[1] #66X722 The copy should be different than the object. Why does it act like if it were just a reference? You need to do a deep copy of your array. Here is the way to do it Marshal.load(Marshal.dump(a)) This is because you are cloning the array but not the elements inside. So the array object is different but the elements it contains

jQuery clone problem

无人久伴 提交于 2019-12-04 03:21:38
问题 I am trying to clone a div and change the names of the input fields in this div. It works great for most of the browsers but IE 7 does not change the name attribute of the input fields. Demo: http://jsbin.com/iduro/7 HTML <body> <pre></pre> <div><input value="Hello World" name="test"></div> </body> JS var lastRow = $("body div:last"), newRow = lastRow.clone(true) .show() .insertAfter(lastRow); newRow.find('input').attr("name","test2"); $("pre").text( newRow[0].innerHTML ); Results: Firefox:

Is there a generic way to forward constructor arguments?

感情迁移 提交于 2019-12-04 03:03:59
I have a working Cloneable/CloneableImpl class pair below. It does its job as long as I have default constructors from child to parent. Suppose Animal's constructor is modified to Animal( std::string const& name ) , which needs the name to be passed up from the children class constructors. How can I incorporate this requirement into the structure while keeping Cloneable/CloneableImpl generic? In other words, I need to be able forward all constructor arguments from Lion, Tiger up to Animal. Is there a way to do this in C++11 in a generic manner? If it's not possible, how can these templates be

How to clone a Java object with the clone() method

不羁的心 提交于 2019-12-04 02:46:54
问题 I don't understand the mechanism of cloning custom object. For example: public class Main{ public static void main(String [] args) { Person person = new Person(); person.setFname("Bill"); person.setLname("Hook"); Person cloned = (Person)person.clone(); System.out.println(cloned.getFname() + " " + cloned.getLname()); } } class Person implements Cloneable{ private String fname; private String lname; public Object clone() { Person person = new Person(); person.setFname(this.fname); person

How to get html of cloned element

笑着哭i 提交于 2019-12-04 02:37:37
I have an input element like: <input class="input-m" name="formelement[]" type="text"> I want to clone it and store cloned html to a variable: var txt=jQuery("input[name="+n+"[]]:first").clone().html(); this returns an empty string. How can I get the html content from .clone() ? Try this var txt=jQuery("input[name="+n+"[]]:first").clone().wrap("<div />").parent().html(); I think you want the outer HTML instead of innerHTML: var text = $('<div>').append(jQuery("input[name="+n+"[]]:first").clone()).html(); .html() returns the html inside the element. I am guessing you have something like <input

git - how can I clone local svn repository?

淺唱寂寞╮ 提交于 2019-12-03 23:50:55
I am completely unable to find any explanation how I should specify location of existing svn repository. In other words - what should be used as URL in git svn clone URL when svn repository is local? You should be able to succeed like this: git svn clone file:///e/svn_repo_on_E_drive Similar to svn checkout command: svn co file:///e/svn_repo_on_E_drive file:// for folder on the current drive of the executing CMD prompt, file:///d/some_folder for D:\some_folder . Note: The extra / and the removed drive colon in the file link on Windows. file://e:/svn_repo_on_E_drive → file:///e/svn_repo_on_E