嘿,大家好,我有这个搜索树结构
class State
{
//CLASS STATE
int value;
char[][] state; //the game Grid
State child[]; // children of current state, maximum is 8
State(char[][] src)
{
state=src;
child=new State[8];
}
这是根节点定义
State rootNode = new State(currentGrid);
rootNode.value=-1;
int v =maxValue(rootNode,depth);
在最大值函数中的递归结束后,rootNode 中的数组不应该被编辑,因为它是第一个状态,但是当我显示它时,我得到一个充满了东西的数组,这意味着 rootNode.state 通过引用传递最大值函数:(
//我正在尝试实现MiniMax算法。
最佳答案
如果您不希望更改作为参数传递的对象,请传入一个副本(或在方法内创建参数的副本)。
请注意,char[][]
意味着您有一个 char 数组数组,即您正在使用对象,如果您复制第一个级别,您仍然可能拥有对第二个级别的引用。
因此,您可能必须循环遍历第一个级别/维度并复制其中的所有数组,如下所示:
char target[][] = new char[state.length][0];
for( int i = 0; i < state.length; ++i ) {
target[i] = Arrays.copyOf(state[i], state[i].length);
}
关于java - 通过引用传递会毁掉一切 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5896374/