gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:52:49 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com