![]() |
why does it change both instances
Alright, so i have a class that contains a 2d array. I make 2 instances of it with the same 2d array arrangement in both of them. If i change one of them the other changes as well for some reason. I can not figure out why both change. Shouldn't only the one instance change?
Here is the node class :
import java.io.*;main where the 2 instances and everything is created :
public class NewMain |
Object variables in Java are references (similar to pointers in C). If you need an independent copy of an object you need to make it; this is usually done by implementing the Cloneable interface and overriding the clone method to create and return a new copy of the object. You might also want to investigate the difference between a "shallow" copy and a "deep" copy.
|
i thought that when a new instance of a class is created that a new copy of each of the methods and variables of that class went along with it. So you see the int[][] state. Lets say when i create a new GridNode, a new int[][] state is not created along with it? Then how do i do this? I do I make it so that when i say
:
GridNode n = new GridNode(initialState, null, null);How do i make it so that it will create a new initialState, another copy of int[][] for the new node. I am going to be doing minor manipulations to each new instance so my goal was to just create a copy of the first instances int[][] into the other ones and then do the manipulations on the other ones. But if i can't just do that with out it taking the old one with it, then what do i do? I have never heard of clonable or anything. i've been stuck on this problem for over 2 days and just want to know what im doing wrong and make it work properly. Thanks. |
Now that I look more closely at your problem, I see that it is not object copying you are having trouble with but rather Java's strange implementation of arrays. Strangely enough, an array in Java is a actually a reference to an "array object" which holds references to its elements. So when you create a new GridNode as you do in the line of code you posted above, the variable n points to a distinct GridNode object. However, when you pass the initialState 2D array as an argument to the constructor, the state variable of the GridNode is a reference the original 2D array. The simplest solution is to simply call the array's clone method to create a new array. (Alternatively you could simply loop over each element and copy it into a new array; see this article for other methods.) The following example might help:
:
int[] p = { 1, 2, 3, 4 }; |
so i think i get what your saying but then how would i make that happen because i am going to be creating like 17 or so of these GridNodes, each with a different 2d array associated to it. Should i have a method in the GridNode class that does this and then call that whenever i create a new instance or something? I tried this just to see if i could get it to work and it didnt.
:
I also tried this and it didnt work either :
GridNode parent = new GridNode(initialState, null, null); |
Okay, you have another (big) problem: When you name a parameter of a method the same as an instance variable of that class, the parameter variable "hides" the instance variable. So, in this snippet of code, for example:
:
public class GridNode:
this.state[row][col] = state[row][col+1]; |
The cause of your problem is the fact that the clone method of a 2D array creates a shallow copy. (Stupid? Yes.) Think of a 2D array as an object with X references to Y 1D arrays (each of which contain additional references--the actual objects stored in the array). The clone method of a 2D array creates a new 2D array--but copies the references to the 1D arrays rather than cloning them! You can avoid this by writing your own method to copy a 2D array:
:
public static int[][] copy(int[][] a) { |
Titanium is giving you the scoop, but you don't seem to get it. Let me make an analogy. Suppose you post a memo on the bulletin board, very top left corner. Someone asks you what the memo says. You refer them to the upper left corner. Someone else asks you what the memo says. You refer them to the upper left corner.
If someone else comes along and scratches out part of the memo and replaces it, all referrals to the upper left corner memo look the same. The ARE the same. That's a reference. If you give someone a note that says, "Upper left corner", and someone else a note that says, "Upper left corner", that's two different notes, but the reference is the same, The second note is a shallow copy of the first. On the other hand, if you copy the memo and hand that out, instead of a reference to the bulletin board, that's a deep copy. If only Joe's is changed, Fred doesn't see that. |
alright so now if i wanted to make a tree with one child and then the child having up to 3 siblings all consisting of GridNodes but each int[][] state of the GridNodes would be different, how would i do that. I get the firstChild points to the child and then the nextSibling would point to the sibling and then i set those to null when i first create the instance and then when needed set them but i need to keep track of what level of the tree im in and all that too. I'm just really lost when it comes to trees so i took a class that deals with them hoping to learn only to find out today that the teacher doesn't help anyone so im actually kinda lost right now.
Alright so this is what i got on how to get it to work like i want it to :
int[][] initialState = {{7, 6, 4},but now i ask if im gonna be creating a bunch of children, is there a way for me to do it without having to create a new corresponding int[][] along with it? Because then to refer to it instead of saying child.getState() to get the state couldnt i just not even use the class and just create a new int [][] within this class and just refer to that for everything? |
i think i got it. This is what the if statement looks like for making the left child of the tree
:
if (parent.canMoveLeft(parent.getState())) |
| All times are GMT -5. The time now is 4:13 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC