public class JavaApplication1 {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
test1(arr1);
for (int i=0; i<arr1.length; i++)
System.out.println(arr1[i]);
test2(arr2);
for (int i=0; i<arr2.length; i++)
System.out.println(arr2[i]);
}
public static void test1(int[] arr1) {
int[] newArr = {4, 5, 6};
arr1 = newArr;
for (int i=0; i<arr1.length; i++)
System.out.println(arr1[i]);}
public static void test2(int[] arr2){
for (int i =0; i<arr2.length; i++)
arr2[i] = i+4;
}
}
输出:
4
5
6
1
2
3
4
5
6
如上,在test1中,
arr1变成了{4, 5, 6},但在方法外Print仍然是原本的{1, 2, 3},
为什么在test2执行后,arr2在方法外也指向{4, 5, 6}了呢?
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
test1(arr1);
for (int i=0; i<arr1.length; i++)
System.out.println(arr1[i]);
test2(arr2);
for (int i=0; i<arr2.length; i++)
System.out.println(arr2[i]);
}
public static void test1(int[] arr1) {
int[] newArr = {4, 5, 6};
arr1 = newArr;
for (int i=0; i<arr1.length; i++)
System.out.println(arr1[i]);}
public static void test2(int[] arr2){
for (int i =0; i<arr2.length; i++)
arr2[i] = i+4;
}
}
输出:
4
5
6
1
2
3
4
5
6
如上,在test1中,
arr1变成了{4, 5, 6},但在方法外Print仍然是原本的{1, 2, 3},
为什么在test2执行后,arr2在方法外也指向{4, 5, 6}了呢?