Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: 2D array shallow and deep cloning

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.'

    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:
    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2D array shallow and deep cloning


  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array shallow and deep cloning

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

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2D array shallow and deep cloning

    You might find this article helpful.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 2D array shallow and deep cloning

    Quote Originally Posted by adrianvas12 View Post
    Yes it is - I need to get an answer since this is drive me insane.
    Crossposting drive me insane!
    Improving the world one idiot at a time!

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

Similar Threads

  1. Help with cloning objects
    By Jumbosize in forum Object Oriented Programming
    Replies: 0
    Last Post: June 11th, 2013, 01:24 PM
  2. how to implement deep cloning without serialization?
    By shreerangpande in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 7th, 2012, 07:04 AM
  3. Stack implementation and shallow copy
    By Shaybay92 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 20th, 2012, 12:32 AM
  4. Array in constructor and deep copy.
    By Ejii in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 08:34 PM