我需要设置两个彼此相等的2d阵列

时间:2021-10-26 11:30:34

Im trying to take a 2D array, which I return in one method, and call it into another method and set a second 2D array equal to the values of the first.

我试图采用一个2D数组,我在一个方法中返回,并将其调用到另一个方法,并设置第二个2D数组等于第一个的值。

This is the first method:

这是第一种方法:

int xx = 23;
int yy = 9;
move(xx,yy, myArray);
myNewArray = myArray;
first = false;

And the second:

第二个:

    public static int[][] move(int x, int y, int[][] myNewArray)
 {
     Scanner input = new Scanner(System.in);
     String direction = input.next();
     boolean moving = true;
     while(moving == true)
     {
         if(direction.indexOf("up") >= 0)
         {
         myNewArray[x][y] = 0;
         myNewArray[x-1][y] = 5;
         moving = false;
         return myNewArray;
        }
        }
    }

Basically I'm trying to take a 2D array and make it into a map, in which you can move the character (the "5") throughout the map by changing the values of where the character is moving to, to 5, and where he is leaving to 0.

基本上我正在尝试使用2D数组并将其制作成地图,在该地图中,您可以通过将字符移动到的位置的值更改为5来移动整个地图中的字符(“5”),以及他要离开0。

Im trying to return an array in the 2nd method and then use it to replace the original array.

我试图在第二种方法中返回一个数组,然后用它来替换原始数组。

2 个解决方案

#1


If you want an actual copy of the contents then use Arrays.copyOf():

What part of Arrays.copyOf() is not clear?

Arrays.copyOf()的哪个部分不清楚?

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

复制指定的数组,使用空值截断或填充(如有必要),以使副本具有指定的长度。对于在原始数组和副本中都有效的所有索引,这两个数组将包含相同的值。对于在副本中有效但不在原始副本中的任何索引,副本将包含null。当且仅当指定的长度大于原始数组的长度时,这些索引才会存在。生成的数组与原始数组完全相同。

If you just want another reference to the reference passed in:

newArray = oldArray;

but this has very little practical use

但这几乎没有实际用途

#2


I think the problem here is that when you're executing move(x,y,array) you're not saving the returned result into another variable. As I think that the arrays are passed by value in Java, this method finnally does nothing.

我认为这里的问题是当你执行move(x,y,array)时,你没有将返回的结果保存到另一个变量中。由于我认为数组是通过Java中的值传递的,因此这种方法最终没有做任何事情。

Try this: int[][] anotherArray = move(x, y, myArray)

试试这个:int [] [] anotherArray = move(x,y,myArray)

A non related issue, but you'll have to be careful with this function, because it will return an ArrayOutOfBounds or a NullPointer Exception if xx ==0

一个非相关的问题,但你必须小心这个函数,因为如果xx == 0它会返回一个ArrayOutOfBounds或NullPointer异常

#1


If you want an actual copy of the contents then use Arrays.copyOf():

What part of Arrays.copyOf() is not clear?

Arrays.copyOf()的哪个部分不清楚?

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

复制指定的数组,使用空值截断或填充(如有必要),以使副本具有指定的长度。对于在原始数组和副本中都有效的所有索引,这两个数组将包含相同的值。对于在副本中有效但不在原始副本中的任何索引,副本将包含null。当且仅当指定的长度大于原始数组的长度时,这些索引才会存在。生成的数组与原始数组完全相同。

If you just want another reference to the reference passed in:

newArray = oldArray;

but this has very little practical use

但这几乎没有实际用途

#2


I think the problem here is that when you're executing move(x,y,array) you're not saving the returned result into another variable. As I think that the arrays are passed by value in Java, this method finnally does nothing.

我认为这里的问题是当你执行move(x,y,array)时,你没有将返回的结果保存到另一个变量中。由于我认为数组是通过Java中的值传递的,因此这种方法最终没有做任何事情。

Try this: int[][] anotherArray = move(x, y, myArray)

试试这个:int [] [] anotherArray = move(x,y,myArray)

A non related issue, but you'll have to be careful with this function, because it will return an ArrayOutOfBounds or a NullPointer Exception if xx ==0

一个非相关的问题,但你必须小心这个函数,因为如果xx == 0它会返回一个ArrayOutOfBounds或NullPointer异常