将数组传递给方法示例毫无意义

时间:2012-12-12 05:22:45

标签: java arrays

我的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 个答案:

答案 0 :(得分: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 discussion

答案 1 :(得分:1)

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

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

实际传递给modifyArray的是数组引用的副本,这就是为什么人们说Java是“通过引用值传递”。

答案 2 :(得分:1)

当您将array传递给modifyArray(int[])时,array实际上不是java primitive,所以array2实际上仍然是与array相同的对象。

复制第一个数组的一种相当简单的方法是使用像这样的系统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。

我希望这会有所帮助。

答案 3 :(得分: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?

   }

请阅读此link以更好地了解