How can I remove an object from its pointer?
I have an object that I have added to several different ArrayLists. I want to avoid changing every list, so I am trying to change the pointer so that all lists change. But I can't get this to work if I want the object to be null. For example:
ArrayList<Point> list1 = new ArrayList<Point>();
ArrayList<Point> list2 = new ArrayList<Point>();
Point a = new Point(2, 3);
list1.add(a); list2.add(a);
At this point, both lists have an element pointing to the same object. With the value "2, 3". If I change the value of the object. Then both lists are modified:
a.set(5,5);
Now both lists have a value element "5, 5". But if I try to set the value to null.
a = null;
Now the parameter is a
set to null, but the object of both lists is still "5, 5" and is not null as I wanted. I even tried adding a method to the class Point
to set it to null. But I couldn't get this to work. What can I do to remove an object in all lists by changing only the pointer?
source to share
What can I do to remove an object in all lists by changing only the pointer?
Nothing. You cannot do it there. If you want to nullify object references in lists (or remove them from lists), you must perform an operation on each of the lists.
If you cannot change the lists, consider the following alternatives:
-
Use another class
Point
that has an extra field that says if point is "valid" and code that traverses the lists checks that field. -
Create a custom class that represents a zero reference for a dot; eg.
public class PointRef { private Point p; public PointRef(Point p) { this.p = p; } public Point get() { return p; } public void invalidate() { p = null; } } ArrayList<PointRef> list1 = new ArrayList<PointRef>(); ArrayList<PointRef> list2 = new ArrayList<PointRef>(); PointRef a = new PointRef(new Point(2, 3)); list1.add(a); list2.add(a); ... a.invalidate; // Now both lists contain hold a PointRef that points to a null Point.
But they both make using lists and their Point objects more complex.
(@ Jeffrey's answer suggests using a class WeakReference
but has incorrect semantics. In particular, the link will "break" if the GC detects that there are no strong references to the object. This is NOT what you are using If you understand your question correctly, then you need to ...)
By the way, I cannot think of ANY programming language that will allow you to do what you are trying to do in the form in which you suggest.
source to share
You can change the state of an object contained in a reference variable, and this will be reflected in all variables that refer to the same object, but if you change the assignment of the reference variable, it will only affect that variable. The specified object does not change - as you would expect.
source to share
In Java, you cannot remove objects from your "pointers" (more precisely, "references"). When an object has been dereferenced (which means: there are no references pointing to it), it becomes a candidate for garbage collection; The Java garbage collector will automatically take care of object disposal for you, there is no operation free()
like in other languages (C / C ++ comes to mind) where you need to manually manage memory.
About your question: you can remove an object from both lists using the remove()
in method , ArrayList
or manually set the object to null
in lists with index ( list.set(index, null)
), but there is no way to remove an object by setting external references (outside the list) to null
.
source to share
As mentioned, there is no easy way to do what you ask. What could you do and I do not recommend using this WeakReference
in your List
s. A weak link will not prevent yours from Point
being garbage collected.
List<WeakReference<Point>> list = new ArrayList<>();
Point p = new Point();
list.add(new WeakReference<>(p));
System.out.println(p);
System.out.println(list.get(0).get());
p = null;
System.gc(); System.gc(); System.gc(); System.gc(); // hopefully the GC collects p by now
System.out.println(list.get(0).get()); // null!
It would be much easier to manually remove
Point
from your List
s.
source to share