What is the purpose of the static “DELETE” object?
In Android classes< a href="http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/util/SparseArray.java/" rel="noreferrer noopener nofollow" > SparseArray, a static final variable DELETED
defined as a simple Object
. Later in the class, its reference is used as an identifier for the deleted entity added to the container. Why are deleted entities not just emptied? What is the purpose of distinguishing between empty and deleted slots?
Note: The SparseArray class is asked directly, asking general questions.
Solution
Because null is a valid value stored in an array, you need to be able to distinguish
between null
and DELETED
.
As an example of why you need to distinguish between them, consider get(key, default)
:
@SuppressWarnings("unchecked")
public E get(int key, E valueIfKeyNotFound) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}
Now if you do something like that:
array.put(1, null);
array.get(1, someDefault);
You will (correctly) get null
.
If you replace DELETED
with null
, you get someDefault
.