在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;
}
I can post more code if I need to, but before that I would like to just ask a general question about the following method in which an array is passed, and then set to another array, but for some reason the original array, the one being passed in, is also getting changed, how this possible/what should i do?
Thanks
tempBoard is an array of same size as currentState, and temp[k] contains the changes that are being made in movePiece, current state is declared in the method and is not a global variable
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Java 中:
引用变量按值传递的最终效果是该引用变量指向的对象或数组 按引用传递。
您的数组实际上是按引用传递的 - 它是同一个数组。
具体来说,MiniMaxBaseCase 中的currentState 是对数组的引用 - 它的值是数组的内存位置。
MiniMaxBaseCase
中的currentState
按值传递给movePiece
,因此MiniMaxBaseCase
中currentState
的值code>(内存位置)如果复制到movePiece
中的参数currentState
- 引用是按值传递的。但现在movePiece
中的currentState
指向与MiniMaxBaseCase
中的currentState
相同的内存位置。所以现在两个变量都指向同一个数组,即最终效果是该数组实际上是按引用传递的。编辑:复制多维数组
有些人一直建议直接使用
System.arraycopy()
或Array.copyOf()
而无需遍历到第一个维度/索引。这行不通。使用这个代替:
直接复制不起作用的原因是因为在 Java 中,二维数组实际上是指向(分散的)一维数组集合的指针/引用的数组,即 int[ ][] 是指向一堆(分散的)
int[]
的指针数组。只需对二维数组执行System.arraycopy()
或Arrays.copyOf()
即可将指针复制到分散的int[]
数组,即这个浅拷贝最终共享底层数组集。您必须执行深层复制,如上面的代码所示。一些参考:
我该怎么办在 Java 中对二维数组进行深度复制?
http://www.cs.dartmouth.edu/~spl/Academic /Java/JFAQs.html
如何复制多维数组?
In Java:
The net effect of a reference variable being passed-by-value is that the object or array pointed to by that reference variable is passed-by-reference.
Your array was effectively passed-by-reference - its the same array.
Specifically,
currentState
inMiniMaxBaseCase
is a reference to an array - the value of it is the memory location of the array.currentState
inMiniMaxBaseCase
is passed by value tomovePiece
, so the value ofcurrentState
inMiniMaxBaseCase
(a memory location) if copied into parametercurrentState
inmovePiece
- the reference was passed by value. But nowcurrentState
inmovePiece
points to the same memory location ascurrentState
inMiniMaxBaseCase
. So now both variables both point to the same array, i.e. the net effect was that the array was effectively passed-by-reference.Edit: Copying multi-dimensional arrays
Some people have been suggesting to use
System.arraycopy()
orArray.copyOf()
directly without traversing into the first dimension/index. This won't work.Use this instead:
The reason a direct copy won't work is because in Java, a 2-dimensional array is really an array of pointers/references to a collection of (scattered) 1-dimensional arrays, i.e.,
int[][]
is an array of pointers to a bunch of (scattered)int[]
. Simply doing aSystem.arraycopy()
orArrays.copyOf()
on a 2-D array will simply copy the pointers to the scatteredint[]
arrays, i.e. this shallow copy ends up sharing the underlying set of arrays. You must do a deep copy as shown in the code above.Some references:
How do I do a deep copy of a 2d array in Java?
http://www.cs.dartmouth.edu/~spl/Academic/Java/JFAQs.html
How do I copy multi-dimensional arrays?
在 Java 中,不存在“按引用传递”这样的东西。您似乎知道这一点,并且想知道为什么仍然感觉这就是这里发生的事情......
问题是,数组是对象。对象实际上是指针。因此,您获得了指向数组的指针的副本,但它仍然是它所指向的数组。
如果要在对数组进行任何更改之前创建数组的副本,请使用 System.arraycopy()。
In Java there is NO SUCH THING as pass-by-reference. You seem to know that and wonder why it still feels like that this is what happens here...
Well the Problem is, that arrays are Objects. And Objects are actually Pointers. So you get a COPY of the pointer to the array, but it's still the array it is pointing to.
Use System.arraycopy() if you want to create a copy of the array before making any changes to it.
花一些时间仔细阅读本文。
http://www.cs.toronto.edu/~dianeh/tutorials/params/
跳到“传递数组”
这是来自课程“148:计算机科学简介”,它应该适合 Java 语言的前两课。
要将数组的副本传递给函数,在 Java 1.6 中,可以使用 Array.copyOf,例如
注意:
Array.copyOf
仅适用于一维数组原语。用在其他任何东西上,它都会为您提供一个独立的数组,所有元素都相同并指向原始数组,无论是对象还是嵌套数组。Take some time to have a good read of this.
http://www.cs.toronto.edu/~dianeh/tutorials/params/
Skip down to "Passing Arrays"
This is from a course "148: Introduction to Computer Science", which should fit around the first 2 lessons on the Java language.
To pass a copy of an array to a function, in Java 1.6, you can use Array.copyOf, e.g
Note:
Array.copyOf
is only good for single-dimension array of primitives. Used on anything else, it gives you an independent array, with all elements IDENTICAL and pointing to the original array, be it Objects or nested arrays.