Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I want to shallow copy it, so can't I just do this:</p> <p>int[] shallow = orig;</p> </blockquote> <p>That's not really a shallow copy. A copy is a discrete entity that is similar to the original, but is not the original item. In your example, what you actually have are two references that are pointing to the same object. When you create a copy, you should have two resulting objects: the original and the copy.</p> <p>Here, anything you do to modify <code>shallow</code> will happen to <code>orig</code> as well since they both point to the same object.</p> <p>"Shallowness" comes into play when the object you are comparing has references to other objects inside it. For example, if you have an array of integers and you create a copy, you now have two arrays which both contain the same integer values:</p> <pre><code>Original Array [0] [1] [2] [3] After copying: [0] &lt;--- Original [0] [1] [1] [3] [2] [4] Copy ---&gt; [3] </code></pre> <p>However, what if you had an array that consists of objects (let's say <code>objArr1</code> and <code>objArr2</code>)? When you do a shallow copy you now have two new array objects, but each corresponding entry between the two arrays points to the same object (because the objects themselves haven't been copied; just the references have).</p> <pre><code>Original Array: [0:]----&gt; [object 0] [1:]----&gt; [object 1] [2:]----&gt; [object 2] [3:]----&gt; [object 3] </code></pre> <p>After copying (notice how the corresponding locations are pointing to the same instances):</p> <pre><code>Original -&gt; [0:]----&gt; [object 0] &lt;----[:0] &lt;- Copy [1:]----&gt; [object 1] &lt;----[:1] [2:]----&gt; [object 2] &lt;----[:2] [3:]----&gt; [object 3] &lt;----[:3] </code></pre> <p>Now if you modify <code>objArr1</code> by replacing an entry or deleting an entry, that same thing <strong>doesn't</strong> happen to <code>objArr2</code>. <strong>However</strong> if you modify the object at <code>objArr1[0]</code>, that is reflected in <code>objArr2[0]</code> as well since those locations point to the <em>same</em> object. So in this case, even though the container objects themselves are distinct, what they contain are references to the same object.</p> <p>When you do a deep copy, you will two new arrays where each corresponding location points to different instances. So essentially you make copies of objects all the way down.</p> <blockquote> <p>My professor said that for primitives, shallow and deep copy are essentially the same, in that we have to copy over each index of the array. </p> </blockquote> <p>The important distinction to make is that when you copy an array of primitives, you are copying the values over exactly. Each time you get a <strong>new</strong> primitive. However, when you have an array of objects, what you <em>really</em> have is an array of references to objects. So when you create a copy, all you have done is create a <strong>new</strong> array that has copies of the references in the original array. However, these new copies of the references still point to the same corresponding objects. This is what's known as a shallow copy. If you deep-copied the array, then the objects that each individual location refers to, will have been copied also. So you would see something like this:</p> <pre><code>Original -&gt; [0:]----&gt; [object 0] Copy -&gt; [0:]----&gt; [copy of object 0] [1:]----&gt; [object 1] [1:]----&gt; [copy of object 1] [2:]----&gt; [object 2] [2:]----&gt; [copy of object 2] [3:]----&gt; [object 3] [3:]----&gt; [copy of object 3] </code></pre> <blockquote> <p>But setting the whole array equals to another array does the same thing, right?</p> </blockquote> <p>No it does not. What you're doing here is simply creating a new reference to an existing array:</p> <pre><code>arr1 -&gt; [0, 1, 2, 3, 4] </code></pre> <p>Now let's say you did <code>arr2 = arr1</code>. What you have is:</p> <pre><code>arr1 -&gt; [0, 1, 2, 3, 4] &lt;- arr2 </code></pre> <p>So here both <code>arr1</code>, and <code>arr2</code> are pointing to the same array. So any modification you perform using <code>arr1</code> will be reflected when you access the array using <code>arr2</code> since you are looking at the same array. This doesn't happen when you make copies.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload