1
While trying to remove items from my SparsArray found the method remove and another method that is the removeAt but what’s the difference?
1
While trying to remove items from my SparsArray found the method remove and another method that is the removeAt but what’s the difference?
2
Well, the removeis just an alias for delete. Then we must compare:
delete vs removeAt
And there’s not much difference. The only difference is that the delete does a key check with binarySearchbefore assigning the empty Object to the key value.
mValues[i] = DELETED;
That one DELETED is an attribute that contains an empty Object. This object is what replaces value to be deleted.
delete()
public void delete(int key) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
mValues[i] = DELETED;
mGarbage = true;
}
}
}
removeAt()
public void removeAt(int index) {
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
}
}
Which one should I use?
If you are SURE that the key that is informed will always exist, use the removeAt.
Otherwise use the remove or delete.
For consultation: Sparsearray, Documentation of Android
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
Then one would be the removal of the key and the other removal of the contents of the
SparseArraythat’s it ?– Ricardo Lucas
@Ricardolucas does not... Both remove the object from the informed key. The difference is that one checks whether the key exists and the other does not.
– Andrei Coelho
@Ricardolucas put the class link for query.
– Andrei Coelho