java - 不需要的对象覆盖
我正在尝试编写一个程序来解决逻辑轮难题。我构建了根节点,并尝试生成通过轮子的不同移动而生成的不同子节点。问题是,当我尝试生成子节点时,根节点被覆盖,一切都一团糟,我真的不知道为什么。 在这里你可以找到这个谜题逻辑轮。
我将轮子表示为3x3 阵列。下面是实现移动的代码:
public Node turn_right(Node aNode, int which_wheel)
{
Node newNode = new Node(aNode.getYellow_wheel(),aNode.getBlue_wheel(),aNode.getGreen_wheel());
int[][] yellow = new int[3][3];
int[][] blue = new int[3][3];
int[][] green = new int[3][3];
if(which_wheel==0) //turn yellow wheel of this node to right
{
yellow[1][0] = newNode.getYellow_wheel()[0][0];
yellow[2][0] = newNode.getYellow_wheel()[1][0];
yellow[2][1] = newNode.getYellow_wheel()[2][0];
yellow[2][2] = newNode.getYellow_wheel()[2][1];
yellow[1][2] = newNode.getYellow_wheel()[2][2];
yellow[0][2] = newNode.getYellow_wheel()[1][2];
yellow[0][1] = newNode.getYellow_wheel()[0][2];
yellow[0][0] = newNode.getYellow_wheel()[0][1];
blue = newNode.getBlue_wheel();
blue[1][0] = newNode.getYellow_wheel()[1][2];
blue[2][0] = newNode.getYellow_wheel()[2][2];
green = newNode.getGreen_wheel();
}
else if(which_wheel == 1)// turn blue wheel of this node to right
{
blue[1][0] = newNode.getBlue_wheel()[0][0];
blue[2][0] = newNode.getBlue_wheel()[1][0];
blue[2][1] = newNode.getBlue_wheel()[2][0];
blue[2][2] = newNode.getBlue_wheel()[2][1];
blue[1][2] = newNode.getBlue_wheel()[2][2];
blue[0][2] = newNode.getBlue_wheel()[1][2];
blue[0][1] = newNode.getBlue_wheel()[0][2];
blue[0][0] = newNode.getBlue_wheel()[0][1];
yellow = newNode.getYellow_wheel();
yellow[0][2] = newNode.getBlue_wheel()[0][0];
yellow[1][2] = newNode.getBlue_wheel()[1][0];
green = newNode.getGreen_wheel();
green[1][0] = newNode.getBlue_wheel()[1][2];
green[2][0] = newNode.getBlue_wheel()[2][2];
}
else if (which_wheel == 2)//turn green wheel of this node to right
{
green[0][0] = newNode.getGreen_wheel()[0][1];
green[0][1] = newNode.getGreen_wheel()[0][2];
green[0][2] = newNode.getGreen_wheel()[1][2];
green[1][2] = newNode.getGreen_wheel()[2][2];
green[2][2] = newNode.getGreen_wheel()[2][1];
green[2][1] = newNode.getGreen_wheel()[2][0];
green[2][0] = newNode.getGreen_wheel()[1][0];
green[1][0] = newNode.getGreen_wheel()[0][0];
yellow = newNode.getYellow_wheel();
blue = newNode.getBlue_wheel();
blue[0][2] = newNode.getGreen_wheel()[0][0];
blue[1][2] = newNode.getGreen_wheel()[1][0];
}
newNode= new Node(yellow,blue,green);
return newNode;
}
还有另一个函数,就像这个函数,它执行相反的操作:它将轮子向左转动。我的问题是我不希望对象的 aNode 表被覆盖。
非常感谢。
I'm trying to make a program that solves the logic wheels puzzle. I construct the root node and I try to produce the different child-nodes that are produced by making different moves of the wheels. The problem is that while I try to produce the children, the root node is overwrited,and everything is messed-up and I really don't know why.
Here you can find the puzzle logic wheels.
I represent the wheels as 3x3 arrays. Here is the code that implements the moves:
public Node turn_right(Node aNode, int which_wheel)
{
Node newNode = new Node(aNode.getYellow_wheel(),aNode.getBlue_wheel(),aNode.getGreen_wheel());
int[][] yellow = new int[3][3];
int[][] blue = new int[3][3];
int[][] green = new int[3][3];
if(which_wheel==0) //turn yellow wheel of this node to right
{
yellow[1][0] = newNode.getYellow_wheel()[0][0];
yellow[2][0] = newNode.getYellow_wheel()[1][0];
yellow[2][1] = newNode.getYellow_wheel()[2][0];
yellow[2][2] = newNode.getYellow_wheel()[2][1];
yellow[1][2] = newNode.getYellow_wheel()[2][2];
yellow[0][2] = newNode.getYellow_wheel()[1][2];
yellow[0][1] = newNode.getYellow_wheel()[0][2];
yellow[0][0] = newNode.getYellow_wheel()[0][1];
blue = newNode.getBlue_wheel();
blue[1][0] = newNode.getYellow_wheel()[1][2];
blue[2][0] = newNode.getYellow_wheel()[2][2];
green = newNode.getGreen_wheel();
}
else if(which_wheel == 1)// turn blue wheel of this node to right
{
blue[1][0] = newNode.getBlue_wheel()[0][0];
blue[2][0] = newNode.getBlue_wheel()[1][0];
blue[2][1] = newNode.getBlue_wheel()[2][0];
blue[2][2] = newNode.getBlue_wheel()[2][1];
blue[1][2] = newNode.getBlue_wheel()[2][2];
blue[0][2] = newNode.getBlue_wheel()[1][2];
blue[0][1] = newNode.getBlue_wheel()[0][2];
blue[0][0] = newNode.getBlue_wheel()[0][1];
yellow = newNode.getYellow_wheel();
yellow[0][2] = newNode.getBlue_wheel()[0][0];
yellow[1][2] = newNode.getBlue_wheel()[1][0];
green = newNode.getGreen_wheel();
green[1][0] = newNode.getBlue_wheel()[1][2];
green[2][0] = newNode.getBlue_wheel()[2][2];
}
else if (which_wheel == 2)//turn green wheel of this node to right
{
green[0][0] = newNode.getGreen_wheel()[0][1];
green[0][1] = newNode.getGreen_wheel()[0][2];
green[0][2] = newNode.getGreen_wheel()[1][2];
green[1][2] = newNode.getGreen_wheel()[2][2];
green[2][2] = newNode.getGreen_wheel()[2][1];
green[2][1] = newNode.getGreen_wheel()[2][0];
green[2][0] = newNode.getGreen_wheel()[1][0];
green[1][0] = newNode.getGreen_wheel()[0][0];
yellow = newNode.getYellow_wheel();
blue = newNode.getBlue_wheel();
blue[0][2] = newNode.getGreen_wheel()[0][0];
blue[1][2] = newNode.getGreen_wheel()[1][0];
}
newNode= new Node(yellow,blue,green);
return newNode;
}
There is another function, like this one that does the oposite:it turns the wheels to left. My problem is that I do not want object's aNode tables to be overwritten.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.clone()
复制您不想覆盖的对象。PS,据我了解您的问题,对
blue = newNode.getBlue_wheel();
的修改也会对 newNode 的蓝轮进行更改,是吗?.clone()
to copy object you not want to overwrite.p.s. as i understand your problem that modifications of
blue = newNode.getBlue_wheel();
also make changes on newNode's blue wheel, is it?好吧,我只需要做这样的事情:
如果您使用 getter 函数并将结果分配给变量,然后尝试更改变量的值,它也会更改您使用其 getter 的对象的值。在我看来这很愚蠢。如果我想改变这样的值,我会使用setter......
Well I just had to make sth like this:
If you use a getter function and assign the result to a variable, and then try change the variable's value, it changes also the value of the object whose getter you used. That seems to me pretty stupid. If I wanted to change such a value, I would use a setter...