Ways to remove with Sparsearray

Asked

Viewed 48 times

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 answer

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

  • Then one would be the removal of the key and the other removal of the contents of the SparseArray that’s it ?

  • @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.

  • 1

    @Ricardolucas put the class link for query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.