Java 创建可修改其值的变量数组

Java 创建可修改其值的变量数组,java,arrays,pointers,reference,Java,Arrays,Pointers,Reference,我想要像这样的东西: 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中可能吗?类似的东西(与此不完全相同)在对象中可能存在: 如果您有数字类: class Number { int value; Number(int value) {

我想要像这样的东西:

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中可能吗?

类似的东西(与此不完全相同)在对象中可能存在:

如果您有数字类:

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
将替换数组中的第一个元素。

在Java中不可能。你想要一些C++引用的东西。java没有它们。@ Kel伍德是的,像C++参考文献,该死的…谢谢你的回答为什么x=5不是4?哇,这很有帮助,谢谢你,我真的很感激你的解决方案。