![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 // set leftChild
public void setLeftChild(GridNode newLeftChild)
{
leftChild = newLeftChild;
} // end setLeftChildWhen 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 |
|
|
|
|
|
#12 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#13 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#14 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
so i could use the clone() method and instead of saying
// set leftChild
public void setLeftChild(GridNode newLeftChild)
{
leftChild = newLeftChild;
} // end setLeftChild // set leftChild
public void setLeftChild(GridNode newLeftChild)
{
leftChild = (GridNode) newLeftChild.clone();
} // end setLeftChild |
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#16 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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?
|
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 (); SomeObject myObjectReference = new SomeObject (); SomeObject newObjectReference; newObjectReference = 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 |
|
|
|
|
|
#18 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#19 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#20 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |