I was thinking of doing it like this, but I'm not sure if the reference temp is assigned will still be valid after removal.
public Box removeBox(int index)
{
Box temp=getBoxes().get(index);//ArrayList.get
getBoxes().remove(index);
return temp;
}
I was thinking of doing it like this, but I'm not sure if the reference temp is assigned will still be valid after removal.
public Box removeBox(int index)
{
Box temp=getBoxes().get(index);//ArrayList.get
getBoxes().remove(index);
return temp;
}
Yes, the reference will still be valid, as there already exists a reference to it (the temp variable you just defined). The JVM's garbage collector will take care of all dereferenced objects when it runs. See this link for more information: How Garbage Collection works in Java, specially When an Object becomes Eligible for Garbage Collection.
ArrayList#remove returns the removed element, if any, so you could also just do return getBoxes().remove(index).