How to iterate through SparseArray?

后端 未结 10 1724
野的像风
野的像风 2020-11-30 18:16

Is there a way to iterate over Java SparseArray (for Android) ? I used sparsearray to easily get values by index. I could not find one.

相关标签:
10条回答
  • 2020-11-30 18:32

    For whoever is using Kotlin, honestly the by far easiest way to iterate over a SparseArray is: Use the Kotlin extension from Anko or Android KTX! (credit to Yazazzello for pointing out Android KTX)

    Simply call forEach { i, item -> }

    0 讨论(0)
  • 2020-11-30 18:37

    Seems I found the solution. I hadn't properly noticed the keyAt(index) function.

    So I'll go with something like this:

    for(int i = 0; i < sparseArray.size(); i++) {
       int key = sparseArray.keyAt(i);
       // get the object by the key.
       Object obj = sparseArray.get(key);
    }
    
    0 讨论(0)
  • 2020-11-30 18:38

    The accepted answer has some holes in it. The beauty of the SparseArray is that it allows gaps in the indeces. So, we could have two maps like so, in a SparseArray...

    (0,true)
    (250,true)
    

    Notice the size here would be 2. If we iterate over size, we will only get values for the values mapped to index 0 and index 1. So the mapping with a key of 250 is not accessed.

    for(int i = 0; i < sparseArray.size(); i++) {
       int key = sparseArray.keyAt(i);
       // get the object by the key.
       Object obj = sparseArray.get(key);
    }
    

    The best way to do this is to iterate over the size of your data set, then check those indeces with a get() on the array. Here is an example with an adapter where I am allowing batch delete of items.

    for (int index = 0; index < mAdapter.getItemCount(); index++) {
         if (toDelete.get(index) == true) {
            long idOfItemToDelete = (allItems.get(index).getId());
            mDbManager.markItemForDeletion(idOfItemToDelete);
            }
        }
    

    I think ideally the SparseArray family would have a getKeys() method, but alas it does not.

    0 讨论(0)
  • 2020-11-30 18:39

    For removing all the elements from SparseArray using the above looping leads to Exception.

    To avoid this Follow the below code to remove all the elements from SparseArray using normal loops

    private void getValues(){      
        for(int i=0; i<sparseArray.size(); i++){
              int key = sparseArray.keyAt(i);
              Log.d("Element at "+key, " is "+sparseArray.get(key));
              sparseArray.remove(key);
              i=-1;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:40

    If you don't care about the keys, then valueAt(int) can be used to while iterating through the sparse array to access the values directly.

    for(int i = 0, nsize = sparseArray.size(); i < nsize; i++) {
        Object obj = sparseArray.valueAt(i);
    }
    
    0 讨论(0)
  • 2020-11-30 18:40

    Simple as Pie. Just make sure you fetch array size before actually performing the loop.

    for(int i = 0, arraySize= mySparseArray.size(); i < arraySize; i++) {
       Object obj = mySparseArray.get(/* int key = */ mySparseArray.keyAt(i));
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题