1 回答
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
查看 的源代碼LongSparseArray,似乎該方法確實(shí)存在 - 但它是隱藏的(由于某種原因):
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* <p>Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
* <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}.
* @hide
*/
public int indexOfValueByValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++) {
if (value == null) {
if (mValues[i] == null) {
return i;
}
} else {
if (value.equals(mValues[i])) {
return i;
}
}
}
return -1;
}
您可以看到所有這些代碼實(shí)際上所做的就是您在問(wèn)題中所說(shuō)的 - 循環(huán)遍歷所有值,直到找到正確的值,然后返回其索引。
Sparse***我不知道為什么它被排除在公共 API 之外,但在我看來(lái),這是反對(duì)使用任何東西的另一點(diǎn)。它們通常太基礎(chǔ),無(wú)法滿足我的要求。
添加回答
舉報(bào)
