# 2D array shallow and deep cloning

• October 13th, 2013, 13:08
2D array shallow and deep cloning
Hello everyone - I am new to Java and trying to understand.
We have an assignment and we have to create a shallow and deep clone using a for loop.
Here is what we have - 'suppose that a is 2D point array but you don't have any idea what data or how many rows or columns it is actually in array a. Write the code which only uses clone in a for loop to make a shallow and a deep clone of a in b using only the references to a and b.'

Code :

```Point [][]a; Point [][]b;```

And this is what the answer is but I am not sure which part is the shallow copy and which is deep:
Code :

```b = (Point[][]) a.clone(); for(int i=0;i<a.lenght;i++) { b[i] = (Point[]) a[i].clone(); for(int j=0;j<b.lenght;j++) b[i][j] = (Point)b[i][j].clone(); }```

Which one is shallow and which one is deep?
I though that
b = (Point[][]) a.clone();
is the shallow but now I am all confused.

Thank you.
AV
• October 13th, 2013, 13:27
GregBrannon
Re: 2D array shallow and deep cloning
• October 13th, 2013, 13:29
Re: 2D array shallow and deep cloning
Yes it is - I need to get an answer since this is drive me insane.
• October 13th, 2013, 16:55
GregBrannon
Re: 2D array shallow and deep cloning
• October 13th, 2013, 20:47
Re: 2D array shallow and deep cloning
Thank you for your feedback - I noticed that article but I was looking more at the clone() method.
I read it few times but I am still not clear.
• October 13th, 2013, 22:26
Junky
Re: 2D array shallow and deep cloning
Quote:

Yes it is - I need to get an answer since this is drive me insane.

Crossposting drive me insane!
• October 14th, 2013, 03:14
GregBrannon
Re: 2D array shallow and deep cloning
It's not clear if you reached a conclusion with your other post. Let us know if you're still working this.

I'm not sure what you mean by "looking more at the clone() method," and it would depend on the clone() method. Since there are pitfalls to using Java's clone() method, many programmers override with their own, more predictable/reliable version.

It would be easy enough to test if you've gotten a shallow or deep copy. Since in a shallow copy there are two references to the same data, changing the data with one reference will cause the other reference to see the change. In a deep copy, there are two sets of data, so changing from one reference does not affect what is seen by the other.
• June 26th, 2016, 13:46
girlycoder
Re: 2D array shallow and deep cloning
This topic was driving me crazy also...;)
Shallow vs. Deep Copy Video Tutorial
Difference between Deep and Shallow Copy in Java Object Cloning
cheers :D
• July 6th, 2016, 01:15
sofitapaul
Re: 2D array shallow and deep cloning
b[i] = (Point[]) a[i].clone();
This line of code is shallow copying as calling clone on an array just copies the references in cells.

for(int j=0;j<b.lenght;j++)
b[i][j] = (Point)b[i][j].clone();

Here it seems you are trying to do deep copying but I guess you need to implement
clone method for Point class accordingly.