Java函数参数值正在改变
我写了一个函数。问题是,我发送的参数正在主程序中被操纵,尽管这不是我的意图。我只是想要函数内部的值,但是在操作时,主程序中的实际值也在改变。
我怎样才能防止这种情况发生?
这是我的代码:
Tiles[][] MoveRight(Tiles[][] tilesArray) {
Tiles[][] tempTilesArray = new Tiles[3][3];
Tiles[][] tempTilesArrayToSend = new Tiles[3][3];
tempTilesArrayToSend = CopyTilesArrays(tilesArray, tempTilesArrayToSend);
ArrayIndex zeroPos = FindZero(tilesArray);
Tiles zeroTile = GetTile(zeroPos, tilesArray);
if (zeroPos.column != 2) {
ArrayIndex otherPos = new ArrayIndex(zeroPos.row,
zeroPos.column + 1);
tempTilesArray = SwapTilesPositions(zeroTile, GetTile(otherPos,
tilesArray), tempTilesArrayToSend);
}
return tempTilesArray;
}
我在 SwapPositionFunction 内发送的数组实际上是在修改tilesArray本身。虽然我已经创建了一个新的tiles数组实例然后发送了它。
I've written a function which. Problem is, the parameters I'm sending, is being manipulated in the main program, though it is not my intention. I just want to have the value inside the function, but while operating, the actual value in the main program is also being changed.
How can I prevent this?
Here is my code:
Tiles[][] MoveRight(Tiles[][] tilesArray) {
Tiles[][] tempTilesArray = new Tiles[3][3];
Tiles[][] tempTilesArrayToSend = new Tiles[3][3];
tempTilesArrayToSend = CopyTilesArrays(tilesArray, tempTilesArrayToSend);
ArrayIndex zeroPos = FindZero(tilesArray);
Tiles zeroTile = GetTile(zeroPos, tilesArray);
if (zeroPos.column != 2) {
ArrayIndex otherPos = new ArrayIndex(zeroPos.row,
zeroPos.column + 1);
tempTilesArray = SwapTilesPositions(zeroTile, GetTile(otherPos,
tilesArray), tempTilesArrayToSend);
}
return tempTilesArray;
}
The array I'm sending inside the SwapPositionFunction is actually modifying the tilesArray itself. Though I've made a new instance of tiles array and then sent it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在没有看到所做的事情的情况下,
我们不能说太多。
请注意,在 Java 中,没有按值传递或按引用传递,而是将引用的副本传递给方法。引用的此副本将(在对象和数组的情况下)指向相同的原始对象,因此如果更改基础/嵌入对象,原始对象会受到影响,但如果更改引用,原始对象不会受到影响做作的。
如果您想传递数组的独立副本,则必须执行深度复制。也许这就是 CopyTilesArrays 应该做的事情,但如果没有看到它,我们不知道。
还要注意,存在,或者更好的是:可以有多层对象,出于不同的原因停留在表面、进入核心或停留在两者之间的某个位置。
例如,要从 Array of Array of Tiles 进行深层复制,您可以执行以下操作:
请注意,在最内层循环中,元素不仅仅通过
fresh[i] = t;< 引用。 /code>,但使用深层复制,以保持原始数组中的对象不受影响。
您可以通过多种其他方式复制 Tiles 数组的数组。例如,您可以重新排列外部数组。如果 Tiles 是,
您可以复制它们,并将目标修改为:
只需复制外部数组并重新排列它们。你可以复制内部数组,如下:
如果你现在将 A 修改为 a,原来的 A 也会受到影响,无需深层复制:
Without seeing what is done in
we can not say much.
Note, that in Java, there is no pass-by-value or pass-by-reference, but a copy of the reference is passed to the methods. This copy of a reference will - in case of objects and Arrays - point to the same, original object, so if you change the underlying/embedded object, the original object is affected, but if you change the reference, the original object is not affected.
IF you want to pass an independent copy of your array, you have to perform a deep ocpy. Maybe that is, what CopyTilesArrays is supposed to do, but without seeing it, we don't know.
Note too, that there are, or better: that there can be several layers of objects, with different reasons to stay on the surface, to go to the core, or to stay somewhere in between.
For example, to make a deep copy from the Array of Array of Tiles, you could do something like this:
Note, that in the innermost loop, the elements aren't just referenced with
fresh[i] = t;
, but with a deep copy, to keep the objects in the original Array unaffected.You could copy an array of arrays of Tiles in multiple other ways. For example, you could rearrange the outer array. If the Tiles were
you could copy them, and modify the target to be:
with just copying the outer arrays, and rearranging them. And you could copy the inner arrays, to be:
If you now modify the A to a, the original A will be affected too, without a deep copy: