簡體   English   中英

創建一個可以修改其值的變量數組

[英]Creating an array of variables whose values can be modified

我想要這樣的東西:

this.x=4;
this.y=5;
this.materials = [this.x, this.y];

this.materials[0]=5;//this will change the x variable

這樣做的結果應該是我原來的x變量的值應該變成 5。

在 Java 中可以使用這樣的變量數組嗎?

對象可以實現類似的(不完全一樣):

如果你有一個 Number 類:

class Number {
    int value;

    Number(int value) {
        this.value = value;
    }
}

你嘗試過這樣的事情:

Number x = new Number(4);
Number y = new Number(5);
Number[] materials = {x, y};

materials[0].value = 5; 
// the value property of the first number object in the array 
// (same as referenced by x) became 5

否則, materials[0] = something只會替換數組中的第一個元素。

暫無
暫無

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

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