请解释此Java数组参考参数传递行为 [英] Please Explain this Java Array Reference Parameter Passing Behavior

查看:95
本文介绍了请解释此Java数组参考参数传递行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class TestArray {
    public static void main(String[] args) {
        int[] ar = {1,2,3,4,5,6,7,8,9};

        shiftRight(ar);
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i]);
        }
        // prints: 912345678 -- good
        System.out.println();

        reverseArray(ar);
        for (int i = 0; i < ar.length; i++) {
            System.out.println(ar[i]);
        }
        // prints: 91234567 -- I don't understand       
        System.out.println();       
    }
    public static void shiftRight(int[] ar) {
        int temp = ar[ar.length - 1];
        for (int i = ar.length - 1; i > 0; i--) {
            ar[i] = ar[i - 1];
        }
        ar[0] = temp;
    }
    public static void reverseArray(int[] ar) {
        int[] temp = new int[ar.length];
        for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
            temp[i] = ar[j];
        }
        ar = temp;
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i]);
        }
        // prints: 876543219
        System.out.println();
    }
}

将数组传递给参数会导致将对数组的引用传递给参数;如果在方法内更改了数组参数,则该更改将在方法外可见.

Passing an array to a parameter results in passing the reference to the array to the parameter; if an array parameter is changed within the method, that change will be visible outside of the method.

第一个方法shiftRight达到了我的期望:它在方法之外更改了数组.

The first method, shiftRight, does what I expect it to: it changes the array outside of the method.

但是,第二种方法不会在方法之外更改数组.但是在方法内部运行for循环会打印正确的值.为什么ar的引用没有指向temp?是因为在方法停止时变量temp被销毁了-也会杀死该引用吗?即使是这种情况,为什么Java会采用指向temp引用的ar,然后将其重新应用为ar的原始引用?

The second method, however, does not change the array outside of the method. But running the for loop inside of the method prints the correct values. Why isn't the reference of ar pointed to temp? Is it because the variable temp is destroyed when the method stops--does that kill the reference as well? Even if this is the case, why does Java take ar, which was pointed to the reference of temp and then reapply it the the original reference of ar?

谢谢.

推荐答案

在Java中,说对象是通过引用传递的,这是不正确的说法.准确地说,对对象的引用是通过值传递的.

In Java, it's a misnomer to say that objects are passed by reference. It's more accurate to say that the reference to the object is passed by value.

按值将数组引用传递给reverseArray.局部参数是对数组的引用的副本.以后当你说

You pass the array reference to reverseArray by value. The local parameter is a copy of the reference to the array. Later when you say

ar = temp;

您仅将 local ar指向temp,而不是main中的原始数组引用ar.

You have only pointed the local ar to temp, not the original array reference ar from main.

另一方面,在shiftRight方法中,您已经通过复制的引用直接访问了数组,因此原始数组的内容发生了变化,并且该方法可以按预期工作.

On the other hand, in the shiftRight method, you have directly accessed the array through the copied reference, so the original array's contents change and the method works as expected.

这篇关于请解释此Java数组参考参数传递行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆