shallow-copy

Is clone() in java shallow copy?

会有一股神秘感。 提交于 2019-12-30 03:15:29
问题 Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). I read this from wikipedia. I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me. 回答1: The default Object.clone() is indeed a shallow copy. However,

Why Object.assign works with an array?

ぐ巨炮叔叔 提交于 2019-12-25 03:22:56
问题 So I was reading an article to clone an object and array. All of them mentioned that Object.assign() can be used to copy an object but no one mentioned that Object.assign() will also work to shallow copy an array. Could anybody explain how it works? Code to Explain is in JS Bin : https://jsbin.com/kaqocixize/edit?js,console 回答1: JS is prototype-oriented language. That means that every construct in JS is in reality - an object! :) Objects differ in some metadata, e.g. specific properties.

Why is copying a list using a slice[:] faster than using the obvious way?

跟風遠走 提交于 2019-12-22 01:53:47
问题 Why is shallow-copying a list using a slice so much faster than using list builtin? In [1]: x = range(10) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 83.2 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 147 ns per loop Usually when I see weird things like this, they're fixed in python3 - but this discrepancy is still there: In [1]: x = list(range(10)) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 100 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

前提是你 提交于 2019-12-21 02:54:55
问题 This error occurs during run time, and I'm not sure what's causing it - the code looks correct to me. #include <iostream> #include <string> using namespace std; struct Room { int d_noSeat; bool d_hasProjector; Room() = default; Room(const Room& r); }; class Event { Room* d_room; std::string d_name; public: Event(); Event(const Event& e); ~Event(); void set(Room r, const std::string& name); void print(); }; Event::Event() : d_room(0), d_name("") {}; void Event::print() { std::cout << "Event: "

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

℡╲_俬逩灬. 提交于 2019-12-20 02:29:12
问题 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

python deepcopy and shallow copy and pass reference

旧时模样 提交于 2019-12-19 10:29:36
问题 A question about python deepcopy and shallow copy. the post at What is the difference between a deep copy and a shallow copy? cannot help me. why e.g. 1 's sum is 6 not 10 ? e.g.1 : kvps = { '1' : 1, '2' : 2 } theCopy = kvps.copy() # both point to the same mem location ? kvps['1'] = 5 sum = kvps['1'] + theCopy['1'] print sum output sum is 6 e.g.2 : aList = [1,2] bList = [3,4] kvps = { '1' : aList, '2' : bList } theCopy = kvps.copy() # both point to the same mem location ? kvps['1'][0] = 5 sum

Shallow clone with JGIT

老子叫甜甜 提交于 2019-12-18 03:54:53
问题 How I can do git clone --depth 1 ... with JGIT library? 回答1: You can't, JGit doesn't yet support shallow clones on the client side (it does on the server side, though). 来源: https://stackoverflow.com/questions/11475263/shallow-clone-with-jgit

Python list slice syntax used for no obvious reason

烂漫一生 提交于 2019-12-17 05:02:32
问题 I occasionally see the list slice syntax used in Python code like this: newList = oldList[:] Surely this is just the same as: newList = oldList Or am I missing something? 回答1: Like NXC said, Python variable names actually point to an object, and not a specific spot in memory. newList = oldList would create two different variables that point to the same object, therefore, changing oldList would also change newList . However, when you do newList = oldList[:] , it "slices" the list, and creates

Networkx copy clarification

放肆的年华 提交于 2019-12-13 13:52:42
问题 According the doc, it appears that the networkx.copy method does a deep copy of the graph. I'm most concerned about the statement This makes a complete copy of the graph including all of the node or edge attributes. Is this suggesting that it makes a copy of what the nodes contain as well? For example if I have the following class NodeContainer(object): def __init__(self, stuff): self.stuff = stuff # ..other class stuff g = networkx.DiGraph(): n1 = NodeContainer(stuff1) n2 = NodeContainer

Shallow Copy of a Custom C# Object

狂风中的少年 提交于 2019-12-13 04:28:35
问题 I am working on some code that is written in C#. In this app, I have a custom collection defined as follows: public class ResultList<T> : IEnumerable<T> { public List<T> Results { get; set; } public decimal CenterLatitude { get; set; } public decimal CenterLongitude { get; set; } } After I query my database and populate a ResultList, I am storing it in in-memory cache. This way I don't need to hit my database every time. This approach works the first time. However, on subsequent loads, it