分配两个彼此相等的数组问题

时间:2021-09-06 11:32:10
public static void main(String[]args) {


    int[] x = {1, 2, 3, 4};
    int[] y ;
    y = x;
    x[1] = 11;
    x = new int[2];

    x[0]=99;
    for (int i = 0; i < y.length; i++)
      System.out.print(y[i] + " ");
    System.out.println("");
    for (int i = 0; i < x.length; i++)
          System.out.print(x[i] + " ");
}

answer is

1 11 3 4 
99 0

My question is I thought when you assign two arrays, they share the same changes since they are objects... like when I set x[1] = 11; it changed the value of y, so shouldn't y still be identical to x after changing it to a 2-sized array, or since I am changing the size they no longer point to the same address?

我的问题是我想当你分配两个数组时,它们共享相同的变化,因为它们是对象...就像我设置x [1] = 11;它改变了y的值,所以在将它改成2个大小的数组之后,不应该仍然与x相同,或者因为我改变了它们不再指向相同地址的大小?

7 个解决方案

#1


8  

int[] x = {1, 2, 3, 4};

{1, 2, 3, 4} is allocated as an array. A reference to it is assigned to x.

{1,2,3,4}被分配为数组。对它的引用分配给x。

int[] y ;
y = x;

A reference to that same array is assigned to y as well.

同样的数组的引用也分配给y。

x[1] = 11;

The array that both x and y refer to is now {1, 11, 3, 4}.

x和y引用的数组现在是{1,11,3,4}。

x = new int[2];

A new array is allocated, due to Java semantics, as {0, 0}. A reference to it is assigned to x. Now x and y refer to two different arrays. There is no resizing being done.

由于Java语义,新数组被分配为{0,0}。对它的引用分配给x。现在x和y指的是两个不同的数组。没有调整大小。

x[0]=99;

The array referred to by x is changed, and now contains {99, 0}. This has nothing to do with the array y refers to, which is still happily {1, 11, 3, 4}.

x引用的数组已更改,现在包含{99,0}。这与y引用的数组无关,它仍然很高兴{1,11,3,4}。

#2


4  

x = new int[2];

This line creates a new array with a length of 2, and then makes x point to that new array. At this point, the original array is only referenced by y.

此行创建一个长度为2的新数组,然后使x指向该新数组。此时,原始数组仅由y引用。

x[0] = 99;

This line assigns the 0th element in that new array to 99, and therefore the original array is left unchanged.

此行将新数组中的第0个元素指定为99,因此原始数组保持不变。

#3


2  

Let me take you through your code line by line

让我逐行引导您完成代码

When you do

当你这样做

Stage #1:

int[] x = {1, 2, 3, 4};

int [] x = {1,2,3,4};

an array object is created inside the HEAP and assigns the address of this INTEGER ARRAY OBJECT to the reference variable x

在HEAP内部创建一个数组对象,并将此INTEGER ARRAY OBJECT的地址分配给引用变量x

Stage #2:

int[] y ;
y = x;

another reference variable y,that can store integer array object is created and is now storing the same address as the previous variable x

另一个引用变量y,可以存储整数数组对象,并且现在存储与前一个变量x相同的地址

Stage #3:

x = new int[2];

with the use of the keyword new we are instructing the JVM to create a new integer array object inside the heap of length 2, and send the address of this newly created object to store inside x.

使用关键字new我们指示JVM在长度为2的堆内创建一个新的整数数组对象,并将此新创建的对象的地址发送到x中。

This means the data (address of the previous object) is now reassigned with the new data (address of the newly created object).

这意味着现在使用新数据(新创建的对象的地址)重新分配数据(前一个对象的地址)。

But this doesn't mean that the address inside the reference variable y is also getting reassigned as well.

但这并不意味着引用变量y中的地址也会被重新分配。

Which means it is still holding the data (address of the previous object) inside it, and when you use the for loop to print both the arrays you get the output as:

这意味着它仍然保留其中的数据(前一个对象的地址),当您使用for循环打印两个数组时,您将获得输出:

1 11 3 4

1 11 3 4

99 0

#4


1  

If you talk about this piece of code..

如果你谈论这段代码..

y = x;  // this actually only reference of same array is going to assign.

In above code only ref. is going to assigned, both x and y is referring to same array object.

在上面的代码中只有参考。将被分配,x和y都指向相同的数组对象。

So, if any changes done in x will be reflected to y automatically.

因此,如果在x中完成的任何更改将自动反映到y。

From here on

从这里开始

 x = new int[2];

x is assigned with new array of size two. So, A totally new array of size 2 is created and assigned to it.

x被赋予大小为2的新数组。因此,创建了一个全新的大小为2的数组并将其分配给它。

so Now on, if any changes in x would not reflect to y and vice-versa.

所以现在,如果x中的任何变化都不会反映到y,反之亦然。

Thank You :)

谢谢 :)

#5


1  

I have commented your code in-line with what is happening with your variables and memory allocation/memory references, let me know if it is clear.

我已经根据您的变量和内存分配/内存引用的内容对您的代码进行了评论,请告知我是否清楚。

public static void main(String[]args) {


    int[] x = {1, 2, 3, 4}; // x -> [1|2|3|4]
    //the reference x points to this data structure in memory
    int[] y ; // y (variable y is not yet pointing to anywhere in memory)
    y = x; // y, x  -> [1|2|3|4] x and y points to the same array
    x[1] = 11; //y,x -> [1|11|3|4] 
    x = new int[2]; //y -> [1|11|3|4] and x -> [0|0]

    x[0]=99; // y -> [1|11|3|4] and x -> [99|0]
    for (int i = 0; i < y.length; i++)
      System.out.print(y[i] + " ");
    System.out.print("");
    for (int i = 0; i < x.length; i++)
          System.out.print(x[i] + " ");
}

#6


1  

After you use the keyword new, a new array object with a different address is created. At line 4: x = new int[2];

使用关键字new后,将创建具有不同地址的新数组对象。第4行:x = new int [2];

So you are assigning a new array object to x. This is no longer the same array that you initialized in line 1. Hence the change in values.

所以你要为x分配一个新的数组对象。这不再是您在第1行中初始化的数组。因此,值的更改。

#7


0  

int[] x = {1, 2, 3, 4};
int[] y ;
y = x;

Till Line no 3 both x and y have same value of array as initialized

直到第3行,x和y都具有与初始化相同的数组值

The format of using new in Java is name = new type [size];

在Java中使用new的格式是name = new type [size];

x = new int[2];

This will allocate memory to x as specified (2 integer values in array). This will overwrite the value which x held earlier

这将按指定的方式将内存分配给x(数组中的2个整数值)。这将覆盖之前持有的x值

Further the default values when array is initialized are 0 for numeric type false for boolean type NULL for reference type

初始化数组时的默认值为0表示数字类型为false表示布尔类型为引用类型为NULL

Thus the array had value {0,0} which was then changed to {99,0}.

因此,数组的值为{0,0},然后更改为{99,0}。

#1


8  

int[] x = {1, 2, 3, 4};

{1, 2, 3, 4} is allocated as an array. A reference to it is assigned to x.

{1,2,3,4}被分配为数组。对它的引用分配给x。

int[] y ;
y = x;

A reference to that same array is assigned to y as well.

同样的数组的引用也分配给y。

x[1] = 11;

The array that both x and y refer to is now {1, 11, 3, 4}.

x和y引用的数组现在是{1,11,3,4}。

x = new int[2];

A new array is allocated, due to Java semantics, as {0, 0}. A reference to it is assigned to x. Now x and y refer to two different arrays. There is no resizing being done.

由于Java语义,新数组被分配为{0,0}。对它的引用分配给x。现在x和y指的是两个不同的数组。没有调整大小。

x[0]=99;

The array referred to by x is changed, and now contains {99, 0}. This has nothing to do with the array y refers to, which is still happily {1, 11, 3, 4}.

x引用的数组已更改,现在包含{99,0}。这与y引用的数组无关,它仍然很高兴{1,11,3,4}。

#2


4  

x = new int[2];

This line creates a new array with a length of 2, and then makes x point to that new array. At this point, the original array is only referenced by y.

此行创建一个长度为2的新数组,然后使x指向该新数组。此时,原始数组仅由y引用。

x[0] = 99;

This line assigns the 0th element in that new array to 99, and therefore the original array is left unchanged.

此行将新数组中的第0个元素指定为99,因此原始数组保持不变。

#3


2  

Let me take you through your code line by line

让我逐行引导您完成代码

When you do

当你这样做

Stage #1:

int[] x = {1, 2, 3, 4};

int [] x = {1,2,3,4};

an array object is created inside the HEAP and assigns the address of this INTEGER ARRAY OBJECT to the reference variable x

在HEAP内部创建一个数组对象,并将此INTEGER ARRAY OBJECT的地址分配给引用变量x

Stage #2:

int[] y ;
y = x;

another reference variable y,that can store integer array object is created and is now storing the same address as the previous variable x

另一个引用变量y,可以存储整数数组对象,并且现在存储与前一个变量x相同的地址

Stage #3:

x = new int[2];

with the use of the keyword new we are instructing the JVM to create a new integer array object inside the heap of length 2, and send the address of this newly created object to store inside x.

使用关键字new我们指示JVM在长度为2的堆内创建一个新的整数数组对象,并将此新创建的对象的地址发送到x中。

This means the data (address of the previous object) is now reassigned with the new data (address of the newly created object).

这意味着现在使用新数据(新创建的对象的地址)重新分配数据(前一个对象的地址)。

But this doesn't mean that the address inside the reference variable y is also getting reassigned as well.

但这并不意味着引用变量y中的地址也会被重新分配。

Which means it is still holding the data (address of the previous object) inside it, and when you use the for loop to print both the arrays you get the output as:

这意味着它仍然保留其中的数据(前一个对象的地址),当您使用for循环打印两个数组时,您将获得输出:

1 11 3 4

1 11 3 4

99 0

#4


1  

If you talk about this piece of code..

如果你谈论这段代码..

y = x;  // this actually only reference of same array is going to assign.

In above code only ref. is going to assigned, both x and y is referring to same array object.

在上面的代码中只有参考。将被分配,x和y都指向相同的数组对象。

So, if any changes done in x will be reflected to y automatically.

因此,如果在x中完成的任何更改将自动反映到y。

From here on

从这里开始

 x = new int[2];

x is assigned with new array of size two. So, A totally new array of size 2 is created and assigned to it.

x被赋予大小为2的新数组。因此,创建了一个全新的大小为2的数组并将其分配给它。

so Now on, if any changes in x would not reflect to y and vice-versa.

所以现在,如果x中的任何变化都不会反映到y,反之亦然。

Thank You :)

谢谢 :)

#5


1  

I have commented your code in-line with what is happening with your variables and memory allocation/memory references, let me know if it is clear.

我已经根据您的变量和内存分配/内存引用的内容对您的代码进行了评论,请告知我是否清楚。

public static void main(String[]args) {


    int[] x = {1, 2, 3, 4}; // x -> [1|2|3|4]
    //the reference x points to this data structure in memory
    int[] y ; // y (variable y is not yet pointing to anywhere in memory)
    y = x; // y, x  -> [1|2|3|4] x and y points to the same array
    x[1] = 11; //y,x -> [1|11|3|4] 
    x = new int[2]; //y -> [1|11|3|4] and x -> [0|0]

    x[0]=99; // y -> [1|11|3|4] and x -> [99|0]
    for (int i = 0; i < y.length; i++)
      System.out.print(y[i] + " ");
    System.out.print("");
    for (int i = 0; i < x.length; i++)
          System.out.print(x[i] + " ");
}

#6


1  

After you use the keyword new, a new array object with a different address is created. At line 4: x = new int[2];

使用关键字new后,将创建具有不同地址的新数组对象。第4行:x = new int [2];

So you are assigning a new array object to x. This is no longer the same array that you initialized in line 1. Hence the change in values.

所以你要为x分配一个新的数组对象。这不再是您在第1行中初始化的数组。因此,值的更改。

#7


0  

int[] x = {1, 2, 3, 4};
int[] y ;
y = x;

Till Line no 3 both x and y have same value of array as initialized

直到第3行,x和y都具有与初始化相同的数组值

The format of using new in Java is name = new type [size];

在Java中使用new的格式是name = new type [size];

x = new int[2];

This will allocate memory to x as specified (2 integer values in array). This will overwrite the value which x held earlier

这将按指定的方式将内存分配给x(数组中的2个整数值)。这将覆盖之前持有的x值

Further the default values when array is initialized are 0 for numeric type false for boolean type NULL for reference type

初始化数组时的默认值为0表示数字类型为false表示布尔类型为引用类型为NULL

Thus the array had value {0,0} which was then changed to {99,0}.

因此,数组的值为{0,0},然后更改为{99,0}。