在下面的代碼中,爲什麼arr2 [0]仍然等於1.5,即使在method2中更改了原始數組?忽略其他數組和變量。數組和參考傳遞
public class Problem3
{
public static int method1(int[] array)
{
array[0] += 10;
return array[0];
}
public static int method2(int aNum, String aStr,
int[] array1, float[] array2, int[] array3)
{
float[] fNums = {1.5F, 2.5F};
array2 = fNums;
return 10 + method1(array3);
}
public static void main(String[] args)
{
int num = 1000;
String aStr = "Hello!";
int[] arr1 = {1, 2, 3};
float[] arr2 = {0.5F, 1.5F};
int[] arr3 = {5, 6, 7};
int retNum = method2(num, aStr, arr1, arr2, arr3);
System.out.println(arr2[0]);
}
}
基本上我以爲答案是1.5,但解決方案說0.5,我想知道如果有人能解釋? – 2012-04-22 20:31:35
看到這個答案:http://stackoverflow.com/a/9404727/597657 – 2012-04-22 20:36:15
在Java中沒有通過引用傳遞 – newacct 2012-04-23 01:20:57