Kai*_*esh 7 java arrays android boolean object
我有一个Boolean类型的ArrayList,需要在我尝试使用时作为boolean []进行操作:
AlertDialog builder;
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { ... });
Run Code Online (Sandbox Code Playgroud)
然而,虽然我可以创建一个布尔对象数组,但我找不到一种有效的方法将这个对象数组转换为构造函数所需的基本数组(我能想到的唯一方法是迭代Object数组并构建一个新的原始数组).
我从ArrayList中检索我的Object数组,如下所示:
final Boolean[] checkedItems = getBoolList().toArray(new Boolean[getBoolList().size()]);
Run Code Online (Sandbox Code Playgroud)
我可以用ArrayList做些什么吗?或者是否有一个我遗漏的明显的转换/转换方法?
任何帮助赞赏!
edw*_*att 10
你没有遗漏任何东西,唯一的方法是迭代列表我害怕
一个(未经测试的)示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
编辑(根据Stephen C的评论):或者您可以使用第三方工具,例如Apache Commons ArrayUtils:
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/ArrayUtils.html
| 归档时间: |
|
| 查看次数: |
10949 次 |
| 最近记录: |