When I tried self-answering this question, I could only find other questions regarding removing items from an array so that items past the removed object get moved down 1 in the index.
In the code below, I have two arrays that hold the class SomeObject (with a name property): SomeObject_ONE and SomeObject_TWO.
Below I put an object "some-object" in the first array, and then set the second array to equal the object in the first array. I can still change the properties of the object in the first array by changing the properties of the object in the second array (the 3rd indent.)
However, when I try setting the object in the second array to null, it doesn't set the first array to null, rather it only removes the index for the object in the second array (4th indent).
Is there anyway to delete an object in an array rather than removing its array index?
Thanks.
SomeObject[] ObjectArray_ONE = new SomeObject[10];
SomeObject[] ObjectArray_TWO = new SomeObject[10];
ObjectArray_ONE[0] = new SomeObject();
ObjectArray_TWO[0] = ObjectArray_ONE[0];
System.out.println(ObjectArray_ONE[0].name+" outputs default name");
ObjectArray_TWO[0].name = "Name changed from OA2";
System.out.println(ObjectArray_ONE[0].name+" outputs Name changed from OA2");
System.out.println("Should not be null : "+ObjectArray_ONE[0]);
ObjectArray_TWO[0] = null;
System.out.println("Should be null : "+ObjectArray_ONE[0]);
ObjectArray_ONE[0] = null;
System.out.println("Only now is it really null: "+ObjectArray_ONE[0]);