Java如何从2D数组中获取1D数组引用

时间:2022-10-14 21:41:31

Say I have a 2D array:

假设我有一个2D数组:

int[][] foo = new int[3][10];

I want to have a better alias name for each 1D array, like:

我希望每个一维数组都有一个更好的别名,比如:

int[] xAxis = foo[0];
int[] yAxis = foo[1];
int[] zAxis = foo[2];

Is xAxis a reference to the 1st 1D array in 2D array? Cause I don't want the array to be copied again. If not, how do I get the reference to 1D arrays.

x轴是二维数组中第一个一维数组的引用吗?因为我不希望数组再次被复制。如果没有,如何获得对1D数组的引用。

5 个解决方案

#1


2  

http://www.functionx.com/java/Lesson22.htm

http://www.functionx.com/java/Lesson22.htm

...with one-dimensional arrays, when passing an multi-dimensional array as argument, the array is treated as a reference. This makes it possible for the method to modify the array and return it changed

…对于一维数组,当将多维数组作为参数传递时,该数组被视为引用。这使得该方法可以修改数组并返回其更改。

when you declare this

当你宣布这个

new int[3][10]

you got one object array with 3 values: array1, array2, array3. All arrays will share the same defined size (10)

你得到一个有3个值的对象数组:array1, array2, array3。所有数组将共享相同的定义大小(10)

so, yes! in this case, foo[0] -> array1[10] , foo[1] -> array2[10] and foo[2] -> array3[10]

所以,是的!在这种情况下,foo[0][10]、foo[1][10]和foo[2] [10]

consider this one: what did you expect if foo[0] didn't pointing to another array? how (foo[x])[y] should works?

考虑这个问题:如果foo[0]没有指向另一个数组,您期望得到什么?如何(foo[x])[y]应该工作吗?

#2


1  

Yes it is reference to 1st Array in 2nd Array. You can verify it by yourself by modifing the xAxis array and check if reflects in 1st 1D array of 2D array.

是的,它指的是第2个数组中的第一个数组。您可以通过修改xAxis数组来验证它,并检查是否在2D数组的第一个1D数组中反射。

#3


1  

Short answer - yes. The statement int[] xAxis = foo[0]; assigns the 1D array in foo[0] to the variable xAxis. Since arrays in Java are objects, this just assigns a reference. The data isn't being copied again.

简短的回答——是的。语句int[] xAxis = foo[0];将foo[0]中的1D数组赋给变量xAxis。因为Java中的数组是对象,所以它只分配一个引用。数据不会再被复制。

#4


1  

Yes, array's are not primitive types, instead they are reference types.

是的,数组不是原始类型,而是引用类型。

So in your case xAxis will be a reference to foo[0]. Which means changes will be visible in both foo[0] and xAxis.

在你的例子中xAxis是foo[0]的引用。这意味着更改将在foo[0]和xAxis中都可见。

But you need to change your first statement to,

但是你需要把第一个表述改成,

int[][] foo = new int[3][10];

#5


0  

I just tried out this code.

我刚试过这段代码。

    int[][] foo = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    int[] xAxis = foo[0];
    int[] yAxis = foo[1];
    int[] zAxis = foo[2];

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            foo[i][j]++;
        }
    }

    for(int i=0;i<3;i++){
        System.out.println("X: " + xAxis[i] + " Y: " + yAxis[i] + " Z: " + zAxis[i]);
    }

And yes, it indeed does reference it.

是的,它确实引用了它。

#1


2  

http://www.functionx.com/java/Lesson22.htm

http://www.functionx.com/java/Lesson22.htm

...with one-dimensional arrays, when passing an multi-dimensional array as argument, the array is treated as a reference. This makes it possible for the method to modify the array and return it changed

…对于一维数组,当将多维数组作为参数传递时,该数组被视为引用。这使得该方法可以修改数组并返回其更改。

when you declare this

当你宣布这个

new int[3][10]

you got one object array with 3 values: array1, array2, array3. All arrays will share the same defined size (10)

你得到一个有3个值的对象数组:array1, array2, array3。所有数组将共享相同的定义大小(10)

so, yes! in this case, foo[0] -> array1[10] , foo[1] -> array2[10] and foo[2] -> array3[10]

所以,是的!在这种情况下,foo[0][10]、foo[1][10]和foo[2] [10]

consider this one: what did you expect if foo[0] didn't pointing to another array? how (foo[x])[y] should works?

考虑这个问题:如果foo[0]没有指向另一个数组,您期望得到什么?如何(foo[x])[y]应该工作吗?

#2


1  

Yes it is reference to 1st Array in 2nd Array. You can verify it by yourself by modifing the xAxis array and check if reflects in 1st 1D array of 2D array.

是的,它指的是第2个数组中的第一个数组。您可以通过修改xAxis数组来验证它,并检查是否在2D数组的第一个1D数组中反射。

#3


1  

Short answer - yes. The statement int[] xAxis = foo[0]; assigns the 1D array in foo[0] to the variable xAxis. Since arrays in Java are objects, this just assigns a reference. The data isn't being copied again.

简短的回答——是的。语句int[] xAxis = foo[0];将foo[0]中的1D数组赋给变量xAxis。因为Java中的数组是对象,所以它只分配一个引用。数据不会再被复制。

#4


1  

Yes, array's are not primitive types, instead they are reference types.

是的,数组不是原始类型,而是引用类型。

So in your case xAxis will be a reference to foo[0]. Which means changes will be visible in both foo[0] and xAxis.

在你的例子中xAxis是foo[0]的引用。这意味着更改将在foo[0]和xAxis中都可见。

But you need to change your first statement to,

但是你需要把第一个表述改成,

int[][] foo = new int[3][10];

#5


0  

I just tried out this code.

我刚试过这段代码。

    int[][] foo = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    int[] xAxis = foo[0];
    int[] yAxis = foo[1];
    int[] zAxis = foo[2];

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            foo[i][j]++;
        }
    }

    for(int i=0;i<3;i++){
        System.out.println("X: " + xAxis[i] + " Y: " + yAxis[i] + " Z: " + zAxis[i]);
    }

And yes, it indeed does reference it.

是的,它确实引用了它。