将布尔对象数组转换为布尔原始数组?
我有一个 Boolean 类型的 ArrayList,需要将其作为 boolean[] 进行操作,因为我正在尝试使用:
AlertDialog builder;
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { ... });
但是,虽然我可以创建一个 Boolean 对象数组,但我找不到一种有效的方法来将此对象数组转换为原始数组构建器函数需要的(我能想到的唯一方法是迭代对象数组并构建一个新的原始数组)。
我正在从 ArrayList 检索对象数组,如下所示:
final Boolean[] checkedItems = getBoolList().toArray(new Boolean[getBoolList().size()]);
我可以用 ArrayList 做些什么吗?或者我是否缺少明显的转换/转换方法?
任何帮助表示赞赏!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有遗漏任何内容,唯一的方法是迭代列表,恐怕是
一个(未经测试的)示例:
编辑(根据 Stephen C 的评论):
或者您可以使用第三方实用程序,例如 Apache Commons ArrayUtils:
You aren't missing anything, the only way to do it is to Iterate over the list I'm afraid
An (Untested) Example:
Edit (as per Stephen C's comment):
Or you can use a third party util such as Apache Commons ArrayUtils:
使用 Guava,您可以执行
boolean[] array = Booleans.toArray(getBoolList());
。Using Guava, you can do
boolean[] array = Booleans.toArray(getBoolList());
.