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

发布于 2024-10-16 04:55:44 字数 971 浏览 5 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2024-10-23 04:55:44

在 Java 中:

  • 方法参数确实是按值传递,但
  • Java 中的所有对象和数组变量都是引用变量嗯>。

引用变量按值传递的最终效果是该引用变量指向的对象或数组 按引用传递

您的数组实际上是按引用传递的 - 它是同一个数组。

具体来说,MiniMaxBaseCase 中的currentState 是对数组的引用 - 它的值是数组的内存位置。 MiniMaxBaseCase 中的 currentState 按值传递给 movePiece,因此 MiniMaxBaseCasecurrentState 的值code>(内存位置)如果复制到 movePiece 中的参数 currentState - 引用是按值传递的。但现在 movePiece 中的 currentState 指向与 MiniMaxBaseCase 中的 currentState 相同的内存位置。所以现在两个变量都指向同一个数组,即最终效果是该数组实际上是按引用传递的。


编辑:复制多维数组

有些人一直建议直接使用 System.arraycopy()Array.copyOf() 而无需遍历到第一个维度/索引。这行不通。

使用这个代替:

public static int[][] copyOf(int[][] original) {
    int[][] copy = new int[original.length][];
    for (int i = 0; i < original.length; i++) {
        copy[i] = Arrays.copyOf(original[i]);
    }
    return copy;
}

直接复制不起作用的原因是因为在 Java 中,二维数组实际上是指向(分散的)一维数组集合的指针/引用的数组,即 int[ ][] 是指向一堆(分散的)int[] 的指针数组。只需对二维数组执行 System.arraycopy()Arrays.copyOf() 即可将指针复制到分散的int[] 数组,即这个浅拷贝最终共享底层数组集。您必须执行深层复制,如上面的代码所示。

一些参考:

我该怎么办在 Java 中对二维数组进行深度复制?

是的,您应该迭代二维布尔数组才能深度复制它。

http://www.cs.dartmouth.edu/~spl/Academic /Java/JFAQs.html
如何复制多维数组?

...
然而,复制二维数组会出现更多问题,因为多维数组表示为数组的数组。当您克隆数组时,这是一个问题。 System.arraycopy() 将为您提供嵌入数组的引用的副本。 ...

In Java:

  • method arguments are indeed passed-by-value, but
  • all object and array variables in Java are reference variables.

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 in MiniMaxBaseCase is a reference to an array - the value of it is the memory location of the array. currentState in MiniMaxBaseCase is passed by value to movePiece, so the value of currentState in MiniMaxBaseCase (a memory location) if copied into parameter currentState in movePiece - the reference was passed by value. But now currentState in movePiece points to the same memory location as currentState in MiniMaxBaseCase. 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() or Array.copyOf() directly without traversing into the first dimension/index. This won't work.

Use this instead:

public static int[][] copyOf(int[][] original) {
    int[][] copy = new int[original.length][];
    for (int i = 0; i < original.length; i++) {
        copy[i] = Arrays.copyOf(original[i]);
    }
    return copy;
}

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 a System.arraycopy() or Arrays.copyOf() on a 2-D array will simply copy the pointers to the scattered int[] 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?

Yes, you should iterate over 2D boolean array in order to deep copy it.

http://www.cs.dartmouth.edu/~spl/Academic/Java/JFAQs.html
How do I copy multi-dimensional arrays?

...
Copying a two dimensional array is however more problematical because multi-dimensional arrays are represented as arrays of arrays. This is an issue when you are cloning an array. System.arraycopy() will give you a copy of the references to the embedded arrays. ...

孤凫 2024-10-23 04:55:44

在 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.

清风夜微凉 2024-10-23 04:55:44

花一些时间仔细阅读本文。

http://www.cs.toronto.edu/~dianeh/tutorials/params/

跳到“传递数组”

数组是引用。这意味着当我们传递一个数组作为参数时,我们传递的是它的句柄或引用。因此,我们可以在方法内更改数组的内容。

这是来自课程“148:计算机科学简介”,它应该适合 Java 语言的前两课。

要将数组的副本传递给函数,在 Java 1.6 中,可以使用 Array.copyOf,例如

tempBoard = movePiece(Arrays.copyOf(currentState, currentState.length), temp[k]);

注意: 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"

Arrays are references. This means that when we pass an array as a parameter, we are passing its handle or reference. So, we can change the contents of the array inside the method.

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

tempBoard = movePiece(Arrays.copyOf(currentState, currentState.length), temp[k]);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文