Java 中判断数组空值个数的方法有:1. 使用 null 查询;2. 使用 Arrays.stream() 和 filter();3. 使用 int[] 和 0 初始化;4. 使用 Optional 和 isPresent();5. 使用 Guava 的 Iterables.filter()。

如何判断 Java 数组中有几个空值
在 Java 中,可以通过以下方法判断数组中有多少个空值:
1. 使用 null 查询
int countNullValues = 0;for (Object element : array) { if (element == null) { countNullValues++; }}
2. 使用 Arrays.stream() 和 filter()
立即学习“Java免费学习笔记(深入)”;
阿里云-虚拟数字人
阿里云-虚拟数字人是什么? …
2 查看详情
int countNullValues = (int) Arrays.stream(array) .filter(Objects::isNull) .count();
3. 使用 int[] 和 0 初始化
int[] countNullValues = new int[1];Arrays.stream(array) .forEach(element -> { if (element == null) { countNullValues[0]++; } });
4. 使用 Optional 和 isPresent()
int countNullValues = 0;for (Object element : array) { Optional
5. 使用 Guava 的 Iterables.size(Iterables.filter(array, Predicates.isNull()))
import com.google.common.collect.Iterables;import com.google.common.base.Predicates;int countNullValues = Iterables.size(Iterables.filter(array, Predicates.isNull()));
以上就是java怎么判断数组有几个孔的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/411842.html
微信扫一扫
支付宝扫一扫