Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解Java数组_Java_Arrays_Pass By Reference_Pass By Value - Fatal编程技术网

理解Java数组

理解Java数组,java,arrays,pass-by-reference,pass-by-value,Java,Arrays,Pass By Reference,Pass By Value,请查看以下代码: public static void main(String[] args) { Random random = new Random(); int[] array = new int[10]; Arrays.setAll(array, operand -> random.nextInt(10)); System.out.println(Arrays.toString(array)); swap(array, 0, 9);

请查看以下代码:

public static void main(String[] args) {
    Random random = new Random();
    int[] array = new int[10];
    Arrays.setAll(array, operand -> random.nextInt(10));
    System.out.println(Arrays.toString(array));
    swap(array, 0, 9);
    System.out.println(Arrays.toString(array));
}

static void swap(int[] array, int i, int j) {
    int temp = array[i]; // pass by value ??
    array[i] = array[j]; // the value of temp doesn't change, why?
    array[j] = temp; // temp == array[i]
}
在方法
交换中到底发生了什么


我需要一个完整的解释和一个低水平

int temp = array[i]; // storing the value in a new variable

array[i] = array[j]; // the value of temp wan't changes because this is a NEW variable and have it's own piece of memory (It's no a reference or pointer like in C)

array[j] = temp; // here the value inside the array is assigned with saved value in temp
编辑:

好的,让我给你看另一个例子:

public class StringHolder {

    private String value;

    public StringHolder(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return getValue();
    }

}
public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0] = new StringHolder("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}
主要方法:

public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0].setValue("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}
输出:

string 2
string 2
string 1
string 2
根据@chokdee的回答,
tmp
是一个新变量,它有自己的内存块

但是当我们对原始变量(
holder[0]
)应用更改时,它也会影响
tmp

array[i] = array[j]; // the value of temp doesn't change, why?
另一个例子:

public class StringHolder {

    private String value;

    public StringHolder(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return getValue();
    }

}
public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0] = new StringHolder("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}
输出:

string 2
string 2
string 1
string 2

提前感谢。

阵列中位置i处的项目与位置j处的项目交换。因此,在返回方法后,以前称为array[i]的项将在array[j]中找到。方法返回后,将在数组[i]中找到以前称为数组[j]的项


temp变量也用作交换空间。

请尝试在较低级别上进行解释

int temp = array[i]; // storing the value in a new variable

array[i] = array[j]; // the value of temp wan't changes because this is a NEW variable and have it's own piece of memory (It's no a reference or pointer like in C)

array[j] = temp; // here the value inside the array is assigned with saved value in temp
将位置
i
处的元素复制到
temp

array[i] = array[j]; // the value of temp doesn't change, why?
将位置
j
处的值复制到位置
i
temp
不会更改,因为它是在此语句中未访问的变量
temp
副本而不是
数组[i]
的别名

array[j] = temp; // temp == array[i]

将上一行中赋值之前位于位置
i
的值复制到位置
j
temp==array[i]
当且仅当方法开始时
array[i]==array[j]

我将回答您的编辑问题

public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0].setValue("string 2");
    System.out.println(tmp); // string 2
    System.out.println(holders[0]); // string 2
}
因为两者都持有对同一对象的引用


在这种情况下,参考由tmp持有。将新的
StringHolder
对象指定给
holders[0]
后,tmp中的引用将转到实际对象。 通常,如果没有
tmp
,垃圾收集器将删除该对象。
最后你有两个不同的
StringHolder
对象。

这感觉很像一个家庭作业问题……你确定吗?我记得在大学时代,有一个几乎和家庭作业一样的问题:)我需要一个完整的解释和一个低水平的回答。当然,这不是家庭作业?如果你能从片段中看出你的推论是什么,以及你不理解什么,人们会很乐意帮助你。在你看到的代码片段中没有什么大的差别,不同的是在第一个示例中你使用的是基元值,在第二个示例中你使用的是对象。请看这不是低水平的。@MuratK。这取决于OP所指的低水平。从OP的理解水平来看,我确信这已经足够低了:)