11

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!

ColinD
  • 108,630
  • 30
  • 201
  • 202
Kaiesh
  • 1,042
  • 2
  • 14
  • 21

2 Answers2

12

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

edwardsmatt
  • 2,034
  • 16
  • 18
  • 1
    +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. – Stephen C Apr 11 '11 at 00:40
  • 1
    @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. – ColinD Apr 11 '11 at 00:55
  • 1
    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) – Stephen C Apr 11 '11 at 22:39
  • 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! – Kaiesh Apr 11 '11 at 23:40
  • You need to check for null value. – John Doe Jan 21 '15 at 08:19
5

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

ColinD
  • 108,630
  • 30
  • 201
  • 202
  • 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 Apr 11 '11 at 23:39