• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deep Cloning Problem : code running twice  RSS feed

 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement clone method for deep clonning of custom linked list(of Rectangle objects). The code works as expected but the Node class is calling the Rectangle clone method twice, which is not correct. Any help with this is greatly appreciated! I am new to both cloning and custom linked list and have been trying to do this for a long time now



 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Always use code tags; I have added the tags to your post because you are new and doesn't it look better
Don't catch that Exception; let it propagate.

How do you know the rectangle clone method is called twice?
 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Always use code tags; I have added the tags to your post because you are new and doesn't it look better
Don't catch that Exception; let it propagate.

How do you know the rectangle clone method is called twice?

Yes, it does look better..Thanks! I wanted to count the number of Rectangle object created in the Rectangle class an when I am trying to do that..its giving twice the number in my cloned list. I then used old fashioned System.out.println() to debug and saw the same thing.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many rectangles did you have at first and how many after cloning? I can't see how you can call clone twice on each rectangle object.

I am off now, so maybe somebody else can take over the discussion.
 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My original list has 3(counted in the constructor part). So, the cloned list has 3 too. But the answer is coming out to be 9, when it should be 6.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have a bit of recursion going on here. Cloning the node will treat the node as the head, and basically, jump node to node until the list is done. Unfortunately, you clone each node recursively, so each clone call will do it as a list using that node as the head.

Henry
 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,
I understand your point but then what is the correct way of doing this?
Thanks
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Effective Java item 11 explains this. You may like to check it there.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prachi vora wrote:
I understand your point but then what is the correct way of doing this?


IMHO, either an iterative or a recursive solution should easily accomplish the task here. However, in this case, it is an iterative *and* recursive solution, which of course, traverses the list a bit too much.


And BTW, technically, it is not double (twice). It just happens that a list of three has six cloned nodes, but a list of size four would have been ten cloned nodes; Likewise five would have been 15, six would have been 21, seven would have been 28, etc. Hopefully, I worked out the equation right.

Henry
 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

Yes, you're right. I was not doing the recursive approach because I tried that on my add method and the stack is full just after 3 elements. Anyways, after you suggestion, I tried the recursion one and that one works. I am still trying to do the iterative one though.

Best,
 
prachi vora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to do the iterative one too! Thanks a lot guys for your help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!