【发布时间】:2011-06-21 01:11:07
【问题描述】:
如果需要,我可以发布更多代码,但在此之前,我只想问一个关于以下方法的一般问题,其中传递一个数组,然后设置为另一个数组,但由于某种原因原始数组,传入的那个也正在改变,这怎么可能/我该怎么办? 谢谢
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;
}
【问题讨论】:
-
数组是对象。对象是引用类型。这并不奇怪。
-
根据到目前为止我所看到的所有地方,在 java 中不可能通过引用传递,但是,可以通过值传递对象引用。这些有什么区别。
-
Jon 的网站对我来说似乎已关闭,但这里是缓存链接:webcache.googleusercontent.com/search?q=cache:http://…
标签: java arrays pass-by-reference