In the Android class SparseArray, a static final variable DELETED is defined as a simple Object. Later in the class its reference is used as an identifier for deleted entities that were added to the container. Why are deleted entities not just nulled out? What is the purpose of differentiating between a null slot and a deleted slot?
Note: while directly asking about the SparseArray class, the question is in general.
It’s because
nullis a valid value to store in the array, so you need to be able to distinguish betweennullandDELETED.As an example of why you need to distinguish them, consider
get(key, default):Now if you do something like:
You will (correctly) get
null.If you replace
DELETEDwithnull, you would getsomeDefaultinstead.