如何删除从ArrayList中或字符串数组的null元素? [英] How to remove all null elements from a ArrayList or String Array?
本文介绍了如何删除从ArrayList中或字符串数组的null元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用这样的循环
// ArrayList tourists
for (Tourist t : tourists) {
if (t != null) {
t.setId(idForm);
}
}
但它是不是很好。任何人都可以建议我一个更好的解决方案?
But it isn't nice. Can anyone suggest me a better solution?
一些有用的基准,以做出更好的决定:
Some useful benchmarks to make better decision:
推荐答案
尝试:
tourists.removeAll(Collections.singleton(null));
阅读的Java API 。在code会抛出 java.lang.UnsupportedOperationException
对不可变列表(如与创建 Arrays.asList
) ;看到这个答案了解更多详情。
Read the Java API. The code will throw java.lang.UnsupportedOperationException
for immutable lists (such as created with Arrays.asList
); see this answer for more details.
这篇关于如何删除从ArrayList中或字符串数组的null元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文