Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 21st, 2007, 9:05 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That's a lot of code to try to make sense of, but this:
   public static GridNode copy(GridNode a)
   {
      GridNode b = a;
      return b;
   } // end copy
will be a shallow copy. In other words, b and a will refer to the same thing. Similarly, here,
   // set leftChild
   public void setLeftChild(GridNode newLeftChild)
   {
      leftChild = newLeftChild;
   } // end setLeftChild
It's difficult to tell, in the second case, whether or not the reference is okay, or a Bad Thang.

When you clone a new GridNode, you need to at least copy the state, not just refer to the source's state.

I also don't know why you're using getters and setters inside the class, where you have access. To me, that just obfuscates things.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 8th, 2007, 12:56 AM   #12
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
its been a while, i know and this and the project's over but just for future reference, this whole shallow copy stuff, i think thats what tripped me up the most, how would i take those 2 methods daWei mentioned and make them make a deep copy rather then a shallow copy? Thanks.
cwl157 is offline   Reply With Quote
Old Oct 8th, 2007, 1:19 AM   #13
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by cwl157 View Post
its been a while, i know and this and the project's over but just for future reference, this whole shallow copy stuff, i think thats what tripped me up the most, how would i take those 2 methods daWei mentioned and make them make a deep copy rather then a shallow copy? Thanks.
I practically wrote you an essay on the topic of shallow vs. deep copy in this thread. As such, I am not particularly inclined to answer the same question again. All that aside, a simple search for "shallow deep copy java" should more than suffice.
titaniumdecoy is offline   Reply With Quote
Old Oct 8th, 2007, 5:58 PM   #14
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
so i could use the clone() method and instead of saying
 // set leftChild
   public void setLeftChild(GridNode newLeftChild)
   {
      leftChild = newLeftChild;
   } // end setLeftChild
i would say something like this (and i havent compile or tried this, i dont have time right now but i just want to get the idea)
 // set leftChild
   public void setLeftChild(GridNode newLeftChild)
   {
      leftChild = (GridNode) newLeftChild.clone();
   } // end setLeftChild
and that will make a seperate exact deep copy, correct? And then whatever i wanted to change in the copy i could and it will only change that one because it is not just a pointer to it, correct?
cwl157 is offline   Reply With Quote
Old Oct 8th, 2007, 6:25 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Java is based heavily on references. You are going to have to come to terms with the implications of that, or you are going to be tilting at windmills for a long time to come.

You still need to be careful with clone. While clone will make a new instance of the object, and copy it, there may be data stuctures that are part of the object that get copied structurally, but their contents may not. In fact, the action of clone varies for differing objects.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 8th, 2007, 7:33 PM   #16
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
wow,this isnt as straight forward as i realized. I thought java was supposed to make it easy to create multiple instances of objects and use them, but i guess not. So your saying that by calling clone like i do that the array could still be a reference and not a new array so i would have to call that method as well to make it its own separate array?
cwl157 is offline   Reply With Quote
Old Oct 8th, 2007, 10:51 PM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm saying you need to investigate the clone method for anything you use it for. See here for Object.clone ().

Read a line like this:
 
ArrayList myArray = new ArrayList ();
as
SomeObject myObjectReference = new SomeObject ();
Obviously, this:
SomeObject newObjectReference;
newObjectReference = myObjectReference;
merely gives you a new reference to the same object referred to by myObjectReference.

Note that if you instantiate a new object for newObjectReference, and then assign myObjectReference to newObjectReference, you have not made a copy. You have just disconnected newObjectReference from the new object and pointed it at the original object.

If you want a new instance of the object, make one. Either clone the original (check the docs for it) or instantiate a new one and copy the contents of the old one over piecemeal. Refer back to my example.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 8th, 2007, 11:06 PM   #18
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
so to copy i should just say
GrideNode node = new GridNode();
GridNode newNode = new GridNode();

// then here i would copy the state from node to new node?
cwl157 is offline   Reply With Quote
Old Oct 9th, 2007, 1:06 AM   #19
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
The way you copy an object may depend on the way its author implemented it. An object's clone method may perform a deep copy or a shallow copy or anything in between. You would have to look at the source or documentation for that object to see how its clone method behaves.

Alternatively, if want to create a deep copy of an object without using its clone method, you would create a new object and set each of its non-basic member variables to reference copies of the other non-basic members of the object you are copying, presumably created in the same way. That is to say, you recurse until there are only basic members left to copy for each object.
titaniumdecoy is offline   Reply With Quote
Old Oct 27th, 2007, 12:35 PM   #20
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
Re: can someone please help me with this tree

alright this is probably stupid, but to make it unique is all i have to do is say this. in the constructor so the constructor would look like this?
// set state to newState and firstChild to newFirstChild and nextSibling to newNextSibling
   public GridNode(int[][] newState, GridNode newLeftChild, GridNode newUpChild, GridNode newRightChild, GridNode newDownChild)
   {
      this.state = newState;
      this.leftChild = newLeftChild;
      this.upChild = newUpChild;
      this.rightChild = newRightChild;
      this. downChild = newDownChild;
   } // end GridNode constructor
cwl157 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
data tree for A* algorithm cwl157 Java 0 Sep 17th, 2007 3:48 PM
Pickle a Class Dietrich Python 9 Nov 17th, 2006 1:38 PM
linked list & binary tree questions n00b C++ 14 Nov 4th, 2006 2:24 AM
Having trouble implementing an int AVL Tree xenop Java 2 Oct 2nd, 2006 7:41 PM
BinaryTree , I need help on inserting expression to the Tree Master C# 3 Oct 14th, 2005 3:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC