6 Replies - 361 Views - Last Post: 19 January 2013 - 10:06 PM Rate Topic: ***-- 2 Votes

#1 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Deep/Shallow copy

Posted 19 January 2013 - 01:38 PM

Hi all,

I built my own Linked List class in java.
each element in the list is of type Node (has next and prev)

However, when I'm trying to remove an element from the list I create a node and assign it to my head Node (the first element in the list)
This is what I'm doing in order to search for the specific element I want to remove.


               Node tmp = head.next;
		while(tmp != null && tmp.data != obj){
			tmp = tmp.next;
		}



My question is:

How come 'head' does not change every-time I change tmp.
It just doesn't make any sense to me.


Thanks a lot,

Tal

Is This A Good Question/Topic? 0
  • +

Replies To: Deep/Shallow copy

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Deep/Shallow copy

Posted 19 January 2013 - 01:44 PM

what does this have to do with Deep/Shallow copy o.o ?
Was This Post Helpful? 0
  • +
  • -

#3 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: Deep/Shallow copy

Posted 19 January 2013 - 01:47 PM

Well,
from what I know when you use shallow copy your new object gets the address of your old object, thus, when you change one of them, the other will be changed as well.
In a deep copy the new object get a new address and only points at the value of the old one.

Isn't it so?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Deep/Shallow copy

Posted 19 January 2013 - 01:57 PM

No
I suggest you do a quick search on Google ^^ I think this one explains it pretty well
Was This Post Helpful? 3
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Deep/Shallow copy

Posted 19 January 2013 - 01:57 PM

You're not changing any objects in your code. Reassigning a variable does not change the object that the variable refers to in any way.

If you modified the object that tmp referred to (by reassigning tmp.next for example), that modification would affect head. But simply reassigning tmp does not.

This post has been edited by sepp2k: 19 January 2013 - 02:02 PM

Was This Post Helpful? 4
  • +
  • -

#6 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: Deep/Shallow copy

Posted 19 January 2013 - 02:10 PM

Allright,

Thank you both.
that was really helpful
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Deep/Shallow copy

Posted 19 January 2013 - 10:06 PM

Avoid the terms deep/shallow copy in Java they are coming from C++ and reflect a different reality.

They don't really make sense as in Java you just have pointers to the actual objects not the objects themselves

When you clone an Object you simply clone (deep copy) the basic data type it holds in its instance variables and symply copy (shallow ???) the Object pointers it contains in the instance variables.

Most of the time this suffices but not all of the times. In some cases you need to clone the objects to which the Object pointers (or some of them) point. This would be done by the code you'll have to write in the clone method that you will have to overload.

And
while(tmp != null && tmp.data != obj){
should be
while(tmp.next != null && tmp.data != obj){
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1