小编jbe*_*ie2的帖子

数组似乎是通过Java引用传递的,这怎么可能?

如果需要,我可以发布更多代码,但在此之前我想问一下关于下面传递数组的方法的一般问题,然后设置为另一个数组,但由于某种原因原始数组,一个被传入,也正在改变,这怎么可能/我该怎么办?谢谢

tempBoard是一个与currentState大小相同的数组,temp [k]包含在movePiece中进行的更改,当前状态在方法中声明,而不是全局变量

private int[][] MiniMaxBaseCase(int[][] currentState, int currentColor)
{
    tempBoard = movePiece(currentState,temp[k]);
}

private int[][] movePiece(int[][] currentState, int[] move)
{
    if(move[0] == -1)
        return currentState;

    //if the piece is just moving
    if(move[4] == -1)
    {
        currentState[move[2]][move[3]] = currentState[move[0]][move[1]];
        currentState[move[0]][move[1]] = 0;
        return currentState;
    }

    //if the piece is jumping another
    if(move[4] != -1)
    {   
        currentState[move[4]][move[5]] = currentState[move[0]][move[1]];
        currentState[move[2]][move[3]] = 0;
        currentState[move[0]][move[1]] = 0;
        return currentState;
    }

    return currentState;
}
Run Code Online (Sandbox Code Playgroud)

java arrays pass-by-reference

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

arrays ×1

java ×1

pass-by-reference ×1