为什么不能简单地使用 arrayA = arrayB 在Java中复制数组?

huangapple go评论80阅读模式
英文:

Why can't simply use arrayA = arrayB to copy array in Java

问题

我知道在Java中复制数组时,需要逐个复制每个元素,但似乎使用arrayA = arrayB,然后修改arrayB后也会修改arrayA。有人能解释一下这是为什么吗?

例如:

int i;
int[] arrayA = new int[]{0, 1, 2, 3, -4, -5, -6, -7};
int[] arrayB = new int[8];

// 直接使用 = 给出了意外的结果
arrayB = arrayA;

for (i = 0; i < arrayA.length; ++i) {
    if (arrayA[i] < 0) {
        arrayA[i] = 0;
    }
}

/*
正常的方法
for (i = 0; i < arrayB.length; ++i) {
    if (arrayB[i] < 0) {
        arrayB[i] = 0;
    }
}
*/

System.out.println("\n原始值和新值:");
for (i = 0; i < arrayA.length; ++i) {
    System.out.println("arrayA:" + arrayA[i] + "   ArrayB:" + arrayB[i]);
}
System.out.println();

输出结果为:

原始值和新值:
arrayA:0   ArrayB:0
arrayA:1   ArrayB:1
arrayA:2   ArrayB:2
arrayA:3   ArrayB:3
arrayA:0   ArrayB:0
arrayA:0   ArrayB:0
arrayA:0   ArrayB:0
arrayA:0   ArrayB:0
英文:

I know when copy array in java one need copy each element individually, but it seems use arrayA = arrayB, and later modify arrayB will also modify arrayA. Could anyone explain why is that?
For example:

  int i;
  int[] arrayA = new int[ ]{0,1,2,3,-4,-5,-6,-7};
  int[] arrayB = new int[8];

  // directly use = gives unexpcted result
  arrayB = arrayA;
  
  for (i = 0; i &lt; arrayA.length; ++i) {
      if (arrayA[i] &lt; 0) {
          arrayA[i] = 0;
      }
  }

  /*
  The normal method
  for (i = 0; i &lt; arrayB.length; ++i) {
     if (arrayB[i] &lt; 0) {
        arrayB[i] = 0;
     }
  }
  */

  System.out.println(&quot;\nOriginal and new values: &quot;);
  for (i = 0; i &lt; arrayA.length; ++i) {
     System.out.println(&quot;arrayA: &quot; +arrayA[i] + &quot;   ArrayB: &quot; + arrayB[i]);
  }
  System.out.println();

The out put is:

Original and new values: 
arrayA: 0   ArrayB: 0
arrayA: 1   ArrayB: 1
arrayA: 2   ArrayB: 2
arrayA: 3   ArrayB: 3
arrayA: 0   ArrayB: 0
arrayA: 0   ArrayB: 0
arrayA: 0   ArrayB: 0
arrayA: 0   ArrayB: 0

答案1

得分: 4

当你说 arrayB = arrayA 时,arrayB 并不是数组本身,它只是指向 arrayA 的引用。所以,如果你想让 arrayB 成为一个独立的数组,你应该这样声明:

int[] arrayB = new int[<数组大小>];

然后,你需要将 arrayA 的每个元素复制到 arrayB 中。

但是,如果你不需要 arrayB 成为一个独立的数组,你无需初始化它,你可以直接这样做:

int[] arrayB = arrayA;

请查看此链接

英文:

When you say arrayB = arrayA, arrayB is not the array itself, it's just a reference to arrayA. So, if you want arrayB to be an independent array, you should declare it

int[] arrayB = new int[&lt;array size&gt;];

Then, you should copy each element of arrayA to arrayB

But if you don't need arrayB to be an independent array, you do not need to initialize it, you could just do

int[] arrayB = arrayA;

Please check out THIS link

答案2

得分: 1

在您的情况下,当不使用 [] 括号引用时,arrayA 和 arrayB 都是引用。

arrayA = arrayB;

这会将 arrayA 的引用更改为指向 arrayB。这指的是数组第一个块的内存地址。直接赋值只是将引用指向该内存分配,但不复制实际值。

这个问题是相关的,您应该查看详细的解释。
https://stackoverflow.com/questions/44732712/array-assignment-and-reference-in-java/44732932

英文:

In your case arrayA and arrayB both are references when referred without the [] brackets.

arrayA = arrayB;

This changes the reference of arrayA to point to arrayB. This refers to the memory address of the first block of the array. Direct assignment simply points the reference to this memory allocation but does not copy actual values.

This question is related and you should check for a detailed explanation.
https://stackoverflow.com/questions/44732712/array-assignment-and-reference-in-java/44732932

huangapple
  • 本文由 发表于 2020年10月22日 08:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64473563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定