Convert SparseBooleanArray to the List which contains indexes whose values are true - Android java.lang

Android examples for java.lang:array convert

Description

Convert SparseBooleanArray to the List which contains indexes whose values are true

Demo Code


//package com.java2s;
import android.util.SparseBooleanArray;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from  ww w .j  a va  2  s.c o  m
     * Convert SparseBooleanArray to the List which contains indexes whose values are true
     * @param sparseCheckedArray
     * @return
     */
    public static List<Integer> convertToCheckedIndexList(
            SparseBooleanArray sparseCheckedArray) {

        List<Integer> checkList = new ArrayList<>();
        for (int i = 0; i < sparseCheckedArray.size(); i++) {
            int index = sparseCheckedArray.keyAt(i);
            if (sparseCheckedArray.valueAt(i)) {
                checkList.add(index);
            }
        }

        return checkList;
    }
}

Related Tutorials