java数组按引用传递不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2831879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:22:27  来源:igfitidea点击:

java array pass by reference does not work?

java

提问by

I thought almost all languages, including java, pass array into function as reference (modifiable).

我认为几乎所有语言,包括 java,都将数组作为引用(可修改)传递给函数。

But somehow it does not work here, and the testArrayis still 1,2,3with size of 3.

但不知何故,它在这里不起作用,并且大小testArray仍然1,2,3为 3。

Strange enough, when if I change result[i] = 2to a[1] =2it works. It did pass by reference.

奇怪的是,当如果我改变result[i] = 2a[1] =2它的工作原理。它确实通过引用传递。

What is wrong with this code?

这段代码有什么问题?

At the end, I had a = result; (which update the a). Did result get removed from stack. Is that why I still get to the original a?

最后,我有a = result; (其中更新了一个)。结果是否从堆栈中删除。这就是为什么我仍然得到原来的a

I am confused.

我很迷惑。

Thanks!

谢谢!

class Test
{
   public static void main(String[] args)
   {

      int[] testArray = {1,2,3};
      equalize(testArray, 6);

      System.out.println("test Array size :" + testArray.length);
      for(int i = 0; i < testArray.length; i++)
         System.out.println(testArray[i]);
   }

   public static void equalize(int[] a, int biggerSize)
   {
      if(a.length > biggerSize)
         throw new Error("Array size bigger than biggerSize");

      int[] result = new int[biggerSize];
     // System.arraycopy(a, 0, result, 0, a.length);
     // int array default value should be 0
      for(int i = 0; i < biggerSize; i++)
         result[i] = 2;

      a = result;
   }
}

回答by Taylor Leese

Java is pass by value. This is why your code does not work. A good practice would be to mark int[] aas finalso this would result in a compilation error (see the corresponding Checkstyle rule).

Java 是按值传递的。这就是您的代码不起作用的原因。一个好的做法是标记int[] afinal这样,这将导致编译错误(请参阅相应的Checkstyle 规则)。

回答by Dean Harding

The array is passed by reference, but the referenceis passed by value. That is, you can change the array that arefers to, but you cannot change which array arefers to.

数组是按引用传递的,但引用按值传递。也就是说,您可以更改a引用的数组,但不能更改引用的数组a

回答by Justin Ardini

The array referenced by acan be modified, but the reference itself is passed by value. So if you did a[0] = 1, then you would be changing the original array. However, a = resultchanges the reference, and so the original reference is unchanged.

引用的数组a可以修改,但引用本身是按值传递的。因此,如果您这样做了a[0] = 1,那么您将更改原始数组。但是,a = result更改了引用,因此原始引用不变。

回答by fastcodejava

回答by Sujee

return parameter "a" from the function and assign to testArray in the main function. When you pass an object by reference, the reference is copied and given to the function. So the object is now referenced by 2 references. Any changes in the object through the 2nd reference will reflect in the first reference, because it is the same object referenced by both of them. But when you change the reference (not the object through reference), it is a different case. you have changed the 2nd reference to point to another object(int[] result). So any changes through the 2nd reference will change only the "result" object.

从函数返回参数“a”并分配给主函数中的 testArray。当您通过引用传递对象时,引用被复制并提供给函数。所以该对象现在被 2 个引用引用。通过第二个引用对对象进行的任何更改都将反映在第一个引用中,因为它们是两个引用的同一个对象。但是当你改变引用(不是通过引用改变对象)时,情况就不同了。您已将第二个引用更改为指向另一个对象(int[] 结果)。因此,通过第二个引用进行的任何更改都只会更改“结果”对象。

class Test
{
   public static void main(String[] args)
   {

      int[] testArray = {1,2,3};
      testArray = equalize(testArray, 6);

      System.out.println("test Array size :" + testArray.length);
      for(int i = 0; i < testArray.length; i++)
         System.out.println(testArray[i]);
   }

   public static int[] equalize(int[] a, int biggerSize)
   {
      if(a.length > biggerSize)
         throw new Error("Array size bigger than biggerSize");

      int[] result = new int[biggerSize];
     // System.arraycopy(a, 0, result, 0, a.length);
     // int array default value should be 0
      for(int i = 0; i < biggerSize; i++)
         result[i] = 2;

      a = result;
      return a;
   }
}

回答by gekrish

Arrays are Objects in java. If you have initialized your array with a size(actually length), you cannot modify it. You can create a new Array with a varying size (as you are currently doing in the function ) and copy all the values from the Old Array to the new Array. In your case, you have 2 objects and 3 references. If you want to access the Array of greater size in the calling function , make the function return the reference of the Array of greater size.

数组是java中的对象。如果您已使用大小(实际上是长度)初始化数组,则无法修改它。您可以创建一个不同大小的新数组(正如您当前在函数中所做的那样)并将旧数组中的所有值复制到新数组中。在您的情况下,您有 2 个对象和 3 个引用。如果要在调用函数中访问更大尺寸的 Array,请让函数返回更大尺寸 Array 的引用。

回答by lulzim

When you do a = result;object adosnt anymore point to the testArray, bc you are changing its reference to result'saddress. That's why it dosnt effect anymore to the testArray. What you are doing actually is you are making athe same adress as resulthas, so whatever you change in ait will change in resulttoo.

当你做一个 = 结果; 对象dosnt不再指向testArray,BC要更改其参考结果的地址。这就是为什么它不再影响testArray。什么你实际上做的是你正在一个同一个地址的结果了,所以无论你在改变一个它会改变结果了。

Hope this helped...

希望这有助于...

回答by Lennotoecom

public class test
{
    public static void main(String[] args){
        int[] a = {15, 2, -3};
        printArray(a);
        changeArray(a);
        printArray(a);
    }
    private static void changeArray(int[] a){
        for(int i = 0; i < a.length; i++){
            a[i]++;
        }
    }

    private static void printArray(int[] a){
        for(int i = 0; i < a.length; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

-----OUTPUT-----

- - -输出 - - -

15 2 -3

15 2 -3

16 3 -2

16 3 -2