This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.'
And this is what the answer is but I am not sure which part is the shallow copy and which is deep:
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.
Welcome to the Ranch
I think what the cryptic post was supposed to mean is that you do not have a 2D array because Java does not support them. You have an array of arrays.
Why are you casting the result of the clone() method? That may be necessary only for the (Point) cast.
The second code block does a deep clone on the array of arrays and also clones each of the Point objects. That should work if Point means java.awt.Point. A poorly‑designed class if ever I saw one
Joined: Oct 13, 2013
I have a better idea now - thank you everyone for your time and effort!.