Coverting a Boolean object array to boolean primitive array?

13,284

Solution 1

You aren't missing anything, the only way to do it is to Iterate over the list I'm afraid

An (Untested) Example:

private boolean[] toPrimitiveArray(final List<Boolean> booleanList) {
    final boolean[] primitives = new boolean[booleanList.size()];
    int index = 0;
    for (Boolean object : booleanList) {
        primitives[index++] = object;
    }
    return primitives;
}

Edit (as per Stephen C's comment): Or you can use a third party util such as Apache Commons ArrayUtils:

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/ArrayUtils.html

Solution 2

Using Guava, you can do boolean[] array = Booleans.toArray(getBoolList());.

Share:
13,284

Related videos on Youtube

Kaiesh
Author by

Kaiesh

Updated on April 12, 2020

Comments

  • Kaiesh
    Kaiesh about 4 years

    I have an ArrayList of type Boolean that requires to be manipulated as a boolean[] as I am trying to use:

    AlertDialog builder;
    builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { ... });
    

    However, while I can create a Boolean object array, I cannot find an efficient way to covert this object array to a primitive array that the builder function calls for (the only method I can come up with is to iterate over the Object array and build a new primitive array).

    I am retrieving my Object array from the ArrayList as follows:

    final Boolean[] checkedItems = getBoolList().toArray(new Boolean[getBoolList().size()]);
    

    Is there something I can do with my ArrayList? Or is there an obvious casting/conversion method that I am missing??

    Any help appreciated!

  • Stephen C
    Stephen C about 13 years
    +1 - There are 3rd-party libraries out there that provide helper methods to do this, but using such a library will bring other issues. So rolling your own method (as above) is the simplest solution.
  • ColinD
    ColinD about 13 years
    @Stephen C: It's the simplest solution if you don't want to use any of the many other useful features of such 3rd party libraries and if you want to write this yourself for each primitive type you want to do this with. Doesn't seem worth it to me. The fewer copy/paste implementations of this sort of thing in the world, the better.
  • Stephen C
    Stephen C about 13 years
    The world can take a flying leap! If you are working under rules that make it hard to use a 3rd party library, a project specific helper method is pragmatically the correct solution. Even if there are no such rules, pulling in external libraries adds a dependency management problem. (Even with Maven ... in some circumstances)
  • Kaiesh
    Kaiesh about 13 years
    Thanks for this, there are so many third party libraries available though - and I fear that I don't make enough use of them to warrant increasing the overall size of my application package.
  • Kaiesh
    Kaiesh about 13 years
    Stephen, I am inclined to agree with you - I don't think I make comprehensive enough use of third party libraries to warrant the extra dependency management and app size increase on my users. I try to use as many native methods as possible!
  • John Doe
    John Doe over 9 years
    You need to check for null value.

Related