[英]What is the purpose of a static “DELETE” object?
In the Android class SparseArray , a static final variable DELETED
is defined as a simple Object
. 在Android类SparseArray中 ,静态最终变量
DELETED
被定义为一个简单的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. 注意:虽然直接询问SparseArray类,但问题仍然存在。
It's because null
is a valid value to store in the array, so you need to be able to distinguish between null
and DELETED
. 这是因为
null
是存储在数组中的有效值,因此您需要能够区分null
和DELETED
。
As an example of why you need to distinguish them, consider get(key, default)
: 作为你需要区分它们的一个例子,考虑
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: 现在,如果您执行以下操作:
array.put(1, null);
array.get(1, someDefault);
You will (correctly) get null
. 你会(正确地)得到
null
。
If you replace DELETED
with null
, you would get someDefault
instead. 如果将
DELETED
替换为null
,则会得到someDefault
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.