Object deletion detection using a pointer

Pay attention to the following code:

ArrayClass<someClass> list = new ArrayList<someClass>();
//Consider this list has been filled somewhere else

someClass selectedObject = null;

public void userAction(float x, float y){
    selectedObject = findObject(x, y);
}

public someClass findObject(float x, float y){
    for(int i=0; i<list.size(); i++)
       if( --objects match-- )
           return list.get(i);

    return null;
}

      

The problem is that I am using this selectedObject somewhere else and I need to know that the object it points to still exists. I noticed that when an object is in the list where the selectObject points are removed, the selectedObjects retains the properties of the object it used to point to (which is no longer there). I need selectObject to point to null after removing the object from the list. How can I achieve this?

EDIT1: To clarify, working code works, this is not a problem. The problem is that the selectedObject pointer is not updated when the object it points to in the list is deleted. One more thing: I have no access to the method that removes objects from the list.

+3


source to share


3 answers


you can achieve this if you make selectedObject a weak reference:

WeakReference<someClass> selectedObject = null;

      

Purpose:

selectedObject = new WeakReference<someClass>(findObject(x, y));

      



request:

someClass v = selectedObject.get();

      

selectedObject get () method returns null if the item is removed from the list (and is not referenced by any other "pointer")

+3


source


If you put an object in List

, it will JVM

store one object and two references, so if you need to be sure it selectedObject

is null, just assign it null

if found:

public someClass findObject(float x, float y){
    for(int i=0; i<list.size(); i++) {
       if( --objects match-- ) {
           selectedObject = null;
           return list.get(i);
       }
    }
    return null;
}

      



Think about it: in your code, you will never remove all ALL references to an object, since you need at least one to return it.

0


source


You can create a wrapper class that will check the list every time you want to access an instance:

class WrapperClass {

    private final List<SomeClass> list;
    private final float x;
    private final float y;


    WrapperClass(List<SomeClass> list, float x, float y) {
        this.list = list;
        this.x = x;
        this.y = y;
    }

    SomeClass get() {
        return findObject();
    }

    private SomeClass findObject(){
        for(int i=0; i<list.size(); i++)
            if( object_match(x, y) {
                return list.get(i);
            }

        return null;
    }

}

      

And what do you call it:

   WrapperClass wp = new WrapperClass(list, 1, 2);
   System.out.println(wp.get()); // returns instance contained in the list

   list.clear();

   System.out.println(wp.get()); // returns null

      

0


source







All Articles