覆盖布尔对象数组到布尔基本数组? [英] Coverting a Boolean object array to boolean primitive array?
问题描述
我有一个Boolean类型的ArrayList,在我尝试使用时,需要将其作为boolean []进行处理:
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() { ... });
但是,尽管我可以创建一个布尔对象数组,但是我找不到一种有效的方法将该对象数组转换为生成器函数需要的原始数组(我唯一能想到的方法是遍历Object.数组并构建一个新的原始数组.
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).
我正在从ArrayList中检索我的Object数组,如下所示:
I am retrieving my Object array from the ArrayList as follows:
final Boolean[] checkedItems = getBoolList().toArray(new Boolean[getBoolList().size()]);
我可以对ArrayList做些什么吗?还是我缺少一种明显的转换/转换方法?
Is there something I can do with my ArrayList? Or is there an obvious casting/conversion method that I am missing??
任何帮助表示赞赏!
推荐答案
您不会丢失任何东西,唯一的方法就是遍历我担心的列表
You aren't missing anything, the only way to do it is to Iterate over the list I'm afraid
(未经测试的)示例:
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;
}
编辑(根据Stephen C的评论): 或者,您可以使用第三方工具,例如Apache Commons ArrayUtils:
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
这篇关于覆盖布尔对象数组到布尔基本数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!