user13842198
user13842198

Reputation:

Remove object from object ArrayList

I have created an object ArrayList,

private ArrayList<Object> objects;

and I am initializing it in a constructor.

public ObjectManager(Handler handler) {
    this.handler = handler;

    objects = new ArrayList<>();
}

This ArrayList is then painted/added it to a canvas.

public void renderObjects(Graphics g) {
    handler.getObjectManager().addObject(new InstanceOfObject(handler, 1000, 1000, g));
}

The method addObject(), adds an object to the ArrayList.

public void addObject(Object e) {
    objects.add(e);
}

I would like to remove this object later, by using a similar line of code,

public void removeObject(Object e) {
    objects.remove(e);
}

however I do not know how to do that because I do not know how to pass in the object that is being removed. The only way I can think of passing in the object is by doing the following:

handler.getObjectManager().removeObject(new InstanceOfObject(handler, 1000, 1000, g));

I don't even know if this would work because it's removing an "new" object. And even if it does, "g" is not defined. If I define it in the constructor, I have to change many different things which results in an error (usually a NullPointerException), but even then I cannot figure out how to call this method by passing in the Graphics g parameters.

Upvotes: 1

Views: 139

Answers (2)

tevemadar
tevemadar

Reputation: 13195

You can actually find Java's source code on the web (like https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/util/ArrayList.java#L644), or even as src.zip in the JDK itself. So this is how remove() looks like:

public boolean remove(Object o) {
    final Object[] es = elementData;
    final int size = this.size;
    int i = 0;
    found: {
        if (o == null) {
            for (; i < size; i++)
                if (es[i] == null)
                    break found;
        } else {
            for (; i < size; i++)
                if (o.equals(es[i]))
                    break found;
        }
        return false;
    }
    fastRemove(es, i);
    return true;
}

and while the loops with the labeled breaks may look a bit esoteric, the important part is the o.equals(): if your "InstanceOfObject" class implements its own equals(), you can make the comparison work with freshly made throwaway instances too.

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338326

Your Question is not clear, but this might help.

The List interface implemented by ArrayList already offers a remove method. No need for you to re-invent that.

Object reference

To remove an object, keep and pass a reference to the particular object.

Dog alice = new Dog( "Alice" , "Labrador" ) ;
Dog bob = new Dog( "Bob" , "Chihuahua" ) ;

List< Dog > dogs = new ArrayList<>() ;
dogs.add( alice ) ;
dogs.add( bob ) ;
…
dogs.remove( bob ) ;

Index number

Alternatively, remember the slot (index) of the list containing the object you want to remove. Pass that zero-based index number to the remove method.

Upvotes: 1

Related Questions