Questions tagged [shallow-copy]

A shallow copy of an object is a duplicate that may not be fully independent of the original, in the sense that any references / pointers it holds to other objects refer to the same objects that the original's do. Use this tag for questions regarding implementing or using shallow copying methods.

Shallow copying is a process by which an object is duplicated to create a copy that may not be wholly independent of the original. Also known as "field by field" copying, this differs from deep copying with respect to the treatment of references / pointers to other objects: shallow copying copies the references / pointers held by the original, whereas deep copying makes a deep copy of each referenced object, too. Shallow copying is "shallow" in the sense that it copies only the original object itself, not any appearing deeper in its reference graph.

Related tags

Links

394 questions
2
votes
2 answers

Question about array shallow copy in C#

Just to make sure I'm understanding shallow copies of reference types correctly and that I'm not constructing a huge memory leak here: // Adds text to the beginning of the log RTB // Also keeps the log RTB trimmed to 100 lines var lines = new…
bszom
2
votes
1 answer

Shallow Copying Class Objects vs. String Variables

string a = "John"; string b = "Doe"; a = b; // Shallow copying strings b = "Elon Musk"; Console.WriteLine(a); // Output: Doe This prints "Doe", meaning the change in one variable is NOT reflected to the other variable. However, when "boxing in"…
Emre Bener
  • 681
  • 3
  • 15
2
votes
3 answers

Unexpected values in two-dimensional array

This is probably a duplicate question but I didn't know how to search for it. Can someone explain the bevior from the code snippet to me? I am trying to iterate over a 2-dimensional array, setting each value to the index of the containing row. Yet…
Felix
  • 369
  • 4
  • 15
2
votes
3 answers

Shallow copy in C

This is my first question. I am learning C. And I heard about shallow copy. As I understood, shallow copy means that you copy the address of the object instead of its value. This means that every change to the copied object will affect the original…
Øuss
  • 55
  • 3
2
votes
2 answers

Java 8 Streams Shallow copy of map object, cross join using streams

My inputs are : List> = [{1=a, 2=b, 3=c}, {1=d, 2=e, 3=f}] List = [x, y, z] Expected Output is : [ {1=a, 2=b, 3=c, 4=x}, {1=a, 2=b, 3=c, 4=y}, {1=a, 2=b, 3=c, 4=z}, {1=d, 2=e, 3=f,…
NightFury
  • 33
  • 4
2
votes
0 answers

JavaScript Spread Operator while making a shallow copy of an array

let arr = [1,2,3] let arr2 = [...arr] let [...arr3] = arr The arr2 and arr3, both make a shallow copy of arr. What is actually the difference?
bbcts
  • 25
  • 3
2
votes
2 answers

How do I share elements between ArrayList and TreeSet in Java?

I want to modify the elements of the ArrayList and TreeSet simultaneously. Ex. When I modify an element from the TreeSet, the corresponding element in the Arraylist is modified too.
2
votes
2 answers

How can I shallow copy a part of list?

I want to shallow copy a part of list, for example: original_list = [0, 1, 2, 3] And I want to copy a part of it list2 = original_list[0:2] list2[0] = 10 wanted output: original_list = [10, 1, 2, 3] But slice only give a new list, Any help would…
Tobiichi
  • 31
  • 6
2
votes
1 answer

What happens to Strings when object is cloned in Java?

By default Java does Shallow Cloning, if an object is cloned and it has a String like this String s = new String("Hi"); then the cloned object will point to a new String object or it will have a reference to the previous object? The question here is…
Wilfred Almeida
  • 601
  • 8
  • 15
2
votes
2 answers

How do I use collections in immutable class safely in Java?

I try to implement immutable class, and I see a rule stating "Perform cloning of objects in the getter methods to return a copy rather than the returning actual object reference". I understand that when we use immutables there would be no change in…
2
votes
1 answer

Issue while making a copy of 2D Array

My target here is to find 'N' for a 2D Array. 'N' = sum of corner elements * sum of non corner elements. For 'N' calculation I change String & Boolean elements to their ASCII, 1 or 0 respectively. But my original array gets altered in this…
2
votes
2 answers

Does the shallow/deep-copy terminology apply for objects without references?

Disclaimer This post is about the correct usage of the terms "shallow-copy" and "deep-copy", specifically when talking about copying an object which does not contain any references. This question is not meant to be (and should not be) opinion-based,…
Felix G
  • 674
  • 1
  • 7
  • 17
2
votes
1 answer

Is shallow copy implementation different for python list and pandas series?

I know the shallow copy behaviour of list list1 = [1, 2, 3, 4] list2 = copy(list1) # shallow copy of list1 list1 is list2 # False as expected False list1[0] is list2[0] # True as expected True list1[0] = 10 # this will change list1 but not…
cryptomanic
  • 5,986
  • 3
  • 18
  • 30
2
votes
3 answers

Cloning a List - how is it done?

I want to make a shallow copy of a List I get returned by a method call (it's public List getScanResults () from Android, see http://developer.android.com/reference/android/net/wifi/WifiManager.html#getScanResults%28%29). The problem is, clone() is…
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
2
votes
2 answers

Deep Copy of object

I have two classes. In one is nested another class. class Person : ICloneable { public string name; public City city; public object Clone() { return this.MemberwiseClone(); } } class City { public string name; …
Rafal
  • 21
  • 1
  • 2