簡體   English   中英

了解Java數組

[英]Understanding Java Arrays

請看下面的代碼:

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]
}

在方法swap到底發生了什么?

我需要一個完整的解釋和一個較低的水平。

編輯:

好,讓我給你看另一個例子:

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].setValue("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}

輸出:

string 2
string 2

根據@chokdee的回答, tmp是一個新變量,具有自己的內存...

但是,當我們將更改應用於原始變量( holder[0] )時,它也會影響tmp

另一個例子 :

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 1
string 2

提前致謝。

可以嘗試從低層次進行解釋。

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 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
}

因為兩者都持有對同一對象的引用。


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); // string 1
    System.out.println(holders[0]); // string 2
}

在這種情況下,引用由tmp保留。 將新的StringHolder對象分配給holders[0] ,tmp中的引用將變為實際對象。 通常,如果沒有tmp則垃圾收集器會刪除該對象。 最后,您有兩個不同的StringHolder對象。

位置i處的數組中的項目與位置j處的項目互換。 因此,返回該方法后,將在array [j]處找到以前稱為array [i]的項。 返回該方法后,將在array [i]處找到以前稱為array [j]的項。

臨時變量也用作交換空間。

int temp = array[i]; // pass by value ??

將位置i的元素復制到temp

array[i] = array[j]; // the value of temp doesn't change, why?

將位置j的值復制到位置i temp不會更改,因為它是此語句中未訪問的變量。 temp副本,而不是array[i]的別名。

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

副本,這在位置值i在以前的行位置賦值之前j temp == array[i]當且僅當方法開始時array[i] == array[j]時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM