deep-copy

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

拈花ヽ惹草 提交于 2019-12-02 02:05:45
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 was being made. Am I right? public class Cloning implements Cloneable { String name; int num; Integer id;

Why does copy of the List still change properties in the original List using C#

徘徊边缘 提交于 2019-12-02 01:38:25
Lets say I have this class public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public bool isActive { get; set; } } And use it like this: List<Employee> Employees = new List<Employee>(); Employees.Add(new Employee { FirstName = "firstname", LastName = "lastname", isActive = true }); List<Employee> EmployeesCopy = new List<Employee>(Employees); EmployeesCopy[0].isActive = false; Why does change in isActive property of EmployeesCopy also modify property in the original list? Because the new list still contains references to the same employee objects

Any alternative to a (very slow) deepcopy in a DFS?

随声附和 提交于 2019-12-02 00:48:36
I have a nice graph (a list) containing 81 vertices (each vertex is an instance of a Vertex class). Each vertex has 20 neighbors. Each vertex has a number of possible values (ranging from 1 to 9) which, given some initial constraints to the problem will be on average 4 or 5. I implemented a simple DFS on this graph, that takes the node with less possible values, foreach value builds another "deepcopied" graph having only one of the possible value, and finally passes the "deepcopied" graph again into the DFS recursively. The issue is about speed; cProfiling my code I found out that 635 of the

How to deep copy (clone) an object with array members in Javascript?

喜你入骨 提交于 2019-12-02 00:16:50
Introduction I have a Class Persons that contains an array of Person and functions : function Persons() { this.mItems = []; // Array of Objects Person } Persons.prototype = { calculateScores : function() { // Do some stuff } } The Class Person has members and functions : function Person(name) { this.name = name; // Name of the Person this.score = 0; } Person.prototype = { calculateScore : function() { // Do some stuff } } I want that the program does the following things : var persons = new Persons(); var person0 = new Person("John"); var person1 = new Person("Sam"); persons.mItems.push

Deep copying an array c# without serialization

删除回忆录丶 提交于 2019-12-01 21:41:27
Basically I have the problem that my MinMax algorithm isn't really working as intended. What I need is to make my array pieces be copied to newPieces so that pieces isn't changed when newPieces is. Here is an extract of the MinMax algorithm: private int MinMax( int depth, Piece[] pieces, bool blacksTurn, List<Move> Moves, Game game, int alpha, Move nextMove) { Piece[] newPieces=new Piece[24]; Moves=Game.possibleMoves(pieces, blacksTurn, false); if(depth==0||Moves.Count==0) { return Evaluation(ref pieces); } int value; if(blacksTurn==true) { foreach(Move i in Moves) { newPieces=DeepCopy

Deep Copy of an Object

随声附和 提交于 2019-12-01 17:21:59
Can I please have some help to perform a deep copy of an object. Here is my code: Option Explicit On Option Strict On <Serializable> Public Class [Class] Private _Name As String Private _ListOfFields As New List(Of Field) Public Property Name As String Get Return _Name End Get Set(value As String) _Name = value End Set End Property Public Property ListOfFields As List(Of Field) Get Return _ListOfFields End Get Set(value As List(Of Field)) _ListOfFields = value End Set End Property Public Function Clone() As [Class] Return DirectCast(Me.MemberwiseClone, [Class]) End Function End Class Field is

Deep Copy of an Object

家住魔仙堡 提交于 2019-12-01 16:23:42
问题 Can I please have some help to perform a deep copy of an object. Here is my code: Option Explicit On Option Strict On <Serializable> Public Class [Class] Private _Name As String Private _ListOfFields As New List(Of Field) Public Property Name As String Get Return _Name End Get Set(value As String) _Name = value End Set End Property Public Property ListOfFields As List(Of Field) Get Return _ListOfFields End Get Set(value As List(Of Field)) _ListOfFields = value End Set End Property Public

copy.deepcopy raises TypeError on objects with self-defined __new__() method

一笑奈何 提交于 2019-12-01 16:05:37
I want to implement a symbol type, which keeps track of the symbols we already have(saved in _sym_table ), and return them if they exist, or create new ones otherwise. The code: # -*- coding: utf-8 -*- _sym_table = {} class Symbol(object): def __new__(cls, sym): if sym not in _sym_table: return super().__new__(cls) else: return _sym_table[sym] def __init__(self, sym): self.sym = sym _sym_table[sym] = self def __str__(self): return self.sym def __cmp__(self, other): return self is other def __hash__(self): return self.sym.__hash__() But when I call copy.deepcopy on a list of such Symbol

Why does jQuery Extend Deep Copy not recursively copy an object?

余生颓废 提交于 2019-12-01 15:53:46
I've searched everywhere and found similar questions with answers that didn't really address my issue so I apologize if this seems like a repeat, but it appears from my experimenting that jQuery's deep copy function doesn't actually work as it's described (or maybe I'm misreading its description). Here's an example demonstrating the problem I'm having: http://jsfiddle.net/wcYsH/ Or this for download: https://github.com/kevroy314/jQuery-Extend-Test Why does the data in the previous copy get changed when the deep copy is manipulated? For one, you aren't creating normal objects. I'm looking at

C# Automatic deep copy of struct

 ̄綄美尐妖づ 提交于 2019-12-01 15:21:53
I have a struct, MyStruct , that has a private member private bool[] boolArray; and a method ChangeBoolValue(int index, bool Value) . I have a class, MyClass , that has a field public MyStruct bools { get; private set; } When I create a new MyStruct object from an existing one, and then apply method ChangeBoolValue(), the bool array in both objects is changed, because the reference, not what was referred to, was copied to the new object. E.g: MyStruct A = new MyStruct(); MyStruct B = A; //Copy of A made B.ChangeBoolValue(0,true); //Now A.BoolArr[0] == B.BoolArr[0] == true Is there a way of