をあなたの配列は、変数が完全に参照しながら、オブジェクトへの参照を保持している場合でも、異なるオブジェクトは配列の内容を変更しません。
コードはオブジェクトを変更しません。変数aを参照してください。 変数をとすると、別のオブジェクトを参照します。
ちょうどあなたのjavascriptのコードのように、次のJavaコードは動作しませんジャバスクリプトのように、Javaは値によってオブジェクトへの参照を渡し、ため:Javaでは
Integer intOne = new Integer(1);
Integer intTwo = new Integer(2);
Integer[] intArray = new Integer[2];
intArray[0] = intOne;
intArray[1] = intTwo;
/* make intTwo refer to a completely new object */
intTwo = new Integer(45);
System.out.println(intArray[1]);
/* output = 2 */
、あなたは変数が参照するオブジェクトを変更した場合(新しい参照を変数に代入するのではなく)あなたが望む動作を得ることができます。
例:
Thing thingOne = new Thing("funky");
Thing thingTwo = new Thing("junky");
Thing[] thingArray = new Thing [2];
thingArray[0] = thingOne;
thingArray[1] = thingTwo;
/* modify the object referenced by thingTwo */
thingTwo.setName("Yippee");
System.out.println(thingArray[1].getName());
/* output = Yippee */
class Thing
{
public Thing(String n) { name = n; }
private String name;
public String getName() { return name; }
public void setName(String s) { name = s; }
}
いいえ、この場合、配列myArray [0] = 'bear''を更新することができます。私はそのような参照を作成する理由を見つけることができません。 – lu1s
私は配列のオブジェクトのプロパティを編集するたびに、私は手動で配列を更新する必要があります。 – indieman
「配列内のオブジェクトのプロパティを編集する」ときに、配列を直接編集しないのはなぜですか? – jahroy