java - 如何检查数组是否为空或数组内容是否为空

标签 java arrays nullpointerexception

在 Java 6 中的 1 个语句中组合检查数组是否为 null 值或数组内容是否为 null 的最佳方法是什么:

if ((myArray[0] != null) || (myArray[1] != null) || (myArray!= null)) {
    ...
}

最佳答案

要让它检查任何值,我会使用 allMatch。首先检查 array != null 也很重要,否则如果是的话,你会得到一个异常。

if (array == null || Arrays.stream(array).allMatch(Objects::isNull)) 

Note that this won't work with java prior to version 8, OP edited his requirements after I posted the answer

关于java - 如何检查数组是否为空或数组内容是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50741800/

相关文章:

java - 如何在 Java 8 中间接运行方法引用?

java - Java 中的静态字符串本地化问题

java - JPA/hibernate : schema generation with multiple persistence units

未处理异常的 Java 问题(带有 lambda 的函数接口(interface))

c - C中的字符串数组非常困惑

c - K & R 的 The C Programming Language 的练习 1-13 中的最后几行代码在做什么?

python - 在 python 中拆分带有日期的数组

java - DisjSet Maze Builder 中出现空指针异常,无法找出原因

java - 如何使用 java 将数据从 CSV 文件加载到表中?

java - 什么是 NullPointerException,我该如何解决?