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
0
votes
1 answer

How to make partial copy of objects in Java?

I have a class and two instances. I want to make partial copy of objects. public class Testing{ String name; String subject; } Testing test1 = new Testing(); test1.setName("myName"); test1.setSubject("mySubject"); Testing test2 = test1;…
Junaid
  • 1,668
  • 7
  • 30
  • 51
0
votes
3 answers

Is it necessary to deep copy an array in java?

As far as I know and researched, arrays in Java are not objects but they're reference-types. My doubt is, when I want to return an array should I return a deep copy (like you would do with an object) with a clone() method for example, or can I…
0
votes
1 answer

Java clone shallow-copy deep-copy copy-constructor nested object

I have written one test app, to demonstrate Java clone using Shallow, Deep and Copy constructor. I achieved with Shallow and Deep but with Copy constructor I think I am missing something. Please look in a code below and let me know the fix for Copy…
Amit Yadav
  • 32,664
  • 6
  • 42
  • 57
0
votes
1 answer

How to perform correctly a deep and a shallow copy in java

1. Definitions - The class Object has a protected method clone which performs a shallow copy of the object, this method should be overridden in any class cloneable. - There is a very particular Cloneable interface, when it's implemented in a class,…
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
0
votes
3 answers

Array deep copy and shallow copy

I'm learning deep copy and shallow copy. If we have two arrays: int[]arr1={1,2,3,4,5}; int[]arr2={1,2,3,4,5}; Question: Both arrays point to the same references [1][2][3][4][5]. What will happen if I change arr1[2]?Does it changes arr2[2]? When we…
walterudoing
  • 115
  • 1
  • 2
  • 10
0
votes
1 answer

Java cloning arrays as parameters in a constructor

As stated in my previous question here, I am attempting to write a game AI. Currently, I have a loop which iterates through the neighbors of a cell, generating a list of possible moves. At some point in this loop (which is heavily recursive), I…
Peaches491
  • 1,239
  • 15
  • 27
0
votes
1 answer

Eclipse warning "Type safety: Unchecked cast" has got recursive solutions

In my case I have this to manipulate a shallow copy of given HashMap public class SomeClass { private HashMap hashMap; public SomeClass( private HashMap hashMap ) { this.hashMap = (HashMap
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
0
votes
1 answer

Does setting one pointer to an object to nil affect the object or other pointers to it?

Although I know the main difference between Deep Copy and Shallow Copy but what I want to ask here is something practical. I have NSArray *firstArray = [NSArray arrayWithObjects:@"first", @"second",@"third", nil]; NSArray *secondArray =…
0
votes
2 answers

Const pointers shallow copy

is it possible to copy MyStruct * const * array1 to MyStruct * array1 but just as a shallow copy? I need sort it and write back into it and I want changes in array 1 too EDIT: Im stupid, I overlooked ** at array1 then it makes sense.
SpeedEX505
  • 1,976
  • 4
  • 22
  • 33
0
votes
3 answers

clone() and system.arracopy() of an array creates two arrays with different references in Java?

Firstly I have to say this is not a duplicate of deep copying vs shallow copying (clone) in Java. but it is related to that. I have read other posts on deep vs shallow copying in SO and while working on this, I found some problem with my…
brain storm
  • 30,124
  • 69
  • 225
  • 393
0
votes
1 answer

shallow copy with generic type

I want to write a generic class that is a First-in-First-out queue with the basic Push and Pop operations, here is the class: class queue { protected final class elem { public E val; public elem next=null; public elem(){} public…
niceman
  • 2,653
  • 29
  • 57
0
votes
1 answer

How to use the same [Storyboard] view for multiple tabs, and just change little things about it depending on the tab selected? (iOS)

I know this question is worded kind of strangely, but let me explain what I'm trying to do: Let's say that I have an app with 2 tabs, each with their own view. Both views are almost exactly the same, except one contains a button or two that the…
giant91
  • 1,153
  • 2
  • 9
  • 18
0
votes
3 answers

Python: writing a function that tests if list1 is a shallow copy of list2

To test if lst1 is a shallow copy of lst2, I did this: def check_shallow_copy(lst1, lst2): ''' Return True if lst1 is a shallow copy of lst2. Return False if not. ''' for idx in range(len(lst1)): if lst1 == lst2 and id(lst1[idx]) ==…
user2489861
  • 109
  • 4
  • 11
0
votes
3 answers

[C ++ pass-by-value]: can the content of the original variables get modified by the called function?

I have always assumed that passing variables with [pass-by-value] in [c++], makes a copy of them, and so the function receiving these copies can not change the original variable's content. I guess it is because when argument is passed by value,…
0
votes
3 answers

How to change the frame of a view when a variable value changes?

In my viewcontroller, there are few views. All of that view's frames are depends on the variable CGFloat borderWidth These views are defined like sec1 = [[MSSectionView alloc]initWithFrame:CGRectMake(self.borderWidth,…
manujmv
  • 6,450
  • 1
  • 22
  • 35