About page 512 list add object


@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

mCurrentBox = new Box(current);
mBoxen.add(mCurrentBox);
break;

case MotionEvent.ACTION_UP:
mCurrentBox = null;
break;
}
}

why after mCurrentBox = null , mCurrentBox .get(position) return correct Box Object;

sorry. my english is very poor.

Sorry, I don’t understand the question. Can you clarify?

Are you asking how the List has all of the boxes when we are setting mCurrentBox to null? If so, this is because the the Box object is referenced by mCurrentBox and by the list (so 2 references). When you set mCurrentBox to null, you remove one of those references but the list still has a reference to the Box object.

So, you’re setting a pointer to null, not the actual object itself.

1 Like

This is a nice explanation. Thank you so much.