0

我的 Java 书中有一个示例程序对我来说毫无意义。基本上它将数组引用传递给方法。但是结果是数组本身被修改了,即使该方法没有返回值或其中的某些东西表明它在做一些事情而不是创建它自己的数组实例。

public class PassArray 
{
   public static void main( String[] args )
   {
      int[] array = { 1, 2, 3, 4, 5 };

      System.out.println( 
         "Effects of passing reference to entire array:\n" +
         "The values of the original array are:" );

      for ( int value : array )
         System.out.printf( "   %d", value );

      modifyArray( array ); // pass array reference to method modifyArray
      System.out.println( "\n\nThe values of the modified array are:" );

      // output the value of array (why did it change?)
      for ( int value : array )
         System.out.printf( "   %d", value );

   } // end main

   // multiply each element of an array by 2 
   public static void modifyArray( int array2[] ) // so this accepts an integer array as an arguement and assigns it to local array array[2]
   {
      for ( int counter = 0; counter < array2.length; counter++ )
         array2[ counter ] *= 2;
   } // What hapened here? We just made changes to array2[] but somehow those changes got applied to array[]??? how did that happen?  

   //What if I wanted to not make any changes to array, how would implement this code so that the output to screen would be the same but the value in array would not change?

} // end class PassArray

请解释为什么会这样,以及如何以某种方式实现这一点,以便不改变数组的值。小号

4

4 回答 4

2

// 这里发生了什么?我们刚刚对 array2[] 进行了更改,但不知何故,这些更改已应用于 array[] ???那是怎么发生的?

因为java是通过引用值传递的。引用的副本将传递给该方法。该引用也仍然指向原始数组。您对此参考执行的任何更改都将反映在原始数组上。

如何以某种方式实现这一点,以便不改变数组的值。

一种方法是,在方法内创建新数组并为其分配此引用。

例子:

public static void modifyArray( int array2[] ) 
   {
      array2 = new int[10];
      //Copy only ten elements from outer array, which populates element at index 2.
      for ( int counter = 0; counter < array2.length; counter++ )
      array2[ counter ] *= 2;
   } 

现在,您对此引用执行的更新/操作将影响在方法内创建的新数组,而不是原始数组。

有关更多信息,请参阅此SO 讨论

于 2012-12-12T05:23:49.933 回答
1

将数组传递给方法时,实际上只是将引用的副本传递给数组。

对数组所做的任何更改都将反映在传递的对象中。

实际上传递给 modifyArray 的是数组引用的副本,这就是人们说 Java 是“按引用值传递”的原因。

于 2012-12-12T05:26:06.237 回答
1

发生的事情是,当您传递arraymodifyArray(int[])isarray实际上不是 java原语时,array2它实际上仍然是与array.

复制第一个数组的一种相当简单的方法是像这样使用 System 的arrayCopy方法

  public static int[] modifyArray( int array[] )
  {
      int length = array.length; // length of the original array
      int array2[] = new int[length]; // the new array which we will copy the data into
      System.arraycopy(array, 0, array2, 0, length); // now we copy the data from array[] into array2[]
      for ( int counter = 0; counter < array2.length; counter++ ) {
          array2[ counter ] *= 2; // multiply by 2
      }

      return array2; // return the array with the new values
  }

您现在拥有原始数组的副本,但所有值都乘以 2。

我希望这有帮助。

于 2012-12-12T05:47:12.107 回答
0
  public class PassArray 
     {
      public static void main( String[] args )
        {
      int[] array = { 1, 2, 3, 4, 5 };

      System.out.println( 
         "Effects of passing reference to entire array:\n" +
         "The values of the original array are:" );

      for ( int value : array )
         System.out.printf( "   %d", value );

      modifyArray( (int[])array.clone(); ); // pass array reference to method modifyArray
      System.out.println( "\n\nThe values of the modified array are:" );

      // output the value of array (why did it change?)
      for ( int value : array )
         System.out.printf( "   %d", value );

   } // end main

   // multiply each element of an array by 2 
   public static void modifyArray( int array2[] ) // so this accepts an integer array as an arguement and assigns it to local array array[2]
   {
      for ( int counter = 0; counter < array2.length; counter++ )
         array2[ counter ] *= 2;
   } // What hapened here? We just made changes to array2[] but somehow those changes got applied to array[]??? how did that happen?  

   //What if I wanted to not make any changes to array, how would implement this code so that the output to screen would be the same but the value in array would not change?

   }

请阅读此链接以更好地理解

于 2012-12-12T05:45:15.603 回答