理解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的理解水平来看,我确信这已经足够低了:)