如何检查空数组java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34847190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check for an empty array java
提问by tamir
I wanted to know if this code is valid for checking whether an array is empty, or should I check for null?
我想知道此代码对于检查数组是否为空是否有效,还是应该检查是否为空?
if(arrayName={})
System.out.println("array empty");
else System.out.println("array not empty");
Thank you!
谢谢!
采纳答案by Gaurav Jeswani
In array class we have a static variable defined "length", which holds the numberof elements in array object. You can use that to find the length as:
在数组类中,我们有一个定义为“length”的静态变量,它保存数组对象中元素的数量。您可以使用它来查找长度为:
if(arrayName.length == 0)
System.out.println("array empty");
else
System.out.println("array not empty");
回答by ostrichofevil
No, because array literals don't work that way. Replace arrayName={}
with arrayName.length==0
, and it should work.
不,因为数组文字不能那样工作。替换arrayName={}
为arrayName.length==0
,它应该可以工作。
回答by soorapadman
To check empty try like this:
要检查空尝试像这样:
if (arr.length == 0) {
System.out.println("array is empty");
}
回答by Omkar
As poited out in the other answers, the length
property of Array
will give the length of the array. But it is always recommended to check if the array is null
before doing any operation on it or else it will throw NullPointerException
if the array is null
.
正如其他答案中指出的那样,length
of的属性Array
将给出数组的长度。但始终建议null
在对其进行任何操作之前检查数组是否为,否则NullPointerException
如果数组为null
.
if (array != null) {
if (array.length == 0)
System.out.println("Empty Array Size=0");
else
System.out.println("Array Not Empty - Size = " + array.length);
} else
System.out.println("array is null");
}
回答by scottb
proudandhonour's answer is on the right track but won't work for all possible values of arrayName
, specifically that case in which arrayName
hasn't been defined and is null
. In that case, the code:
骄傲和荣誉的答案是在正确的轨道上,但不适用于 的所有可能值arrayName
,特别是在arrayName
尚未定义且为 的情况下null
。在这种情况下,代码:
if(arrayName.length == 0)
System.out.println("array empty");
else
System.out.println("array not empty");
will fail with a NullPointerException.
TimeTravel's answer correctly tests for this case and correctly handles all possible values for arrayName
. The only downside is that his code is more verbose than it needs to be.
将因NullPointerException.
TimeTravel 的答案而失败,正确测试此案例并正确处理 的所有可能值arrayName
。唯一的缺点是他的代码比它需要的更冗长。
Java provides short circuit evaluationof Boolean expressions. Specifically the result of (false && xx)
is false
for all possible values of xx
. Therefore when evaluating the first operand of a Boolean &&
operator, the JVM will ignore the 2nd operand if the 1st evaluates to false.
Java 提供布尔表达式的短路评估。具体的结果(false && xx)
是false
对的所有可能值xx
。因此,在计算布尔&&
运算符的第一个操作数时,如果第一个操作数为假,JVM 将忽略第二个操作数。
Exploiting this, we can write:
利用这一点,我们可以写:
if (arrayName != null && arrayName.length > 0)
{ System.out.println("The array length > 0"); }
else
{ System.out.println("The array is null or empty"); }
There is also the ternary operatorwhich provides a mechanism for inlining if-then-else expressions. This can improve readability in some circumstances:
还有三元运算符,它提供了一种内联 if-then-else 表达式的机制。在某些情况下,这可以提高可读性:
System.out.println((arrayName == null)
? "The arrayName variable is null"
: (arrayName.length < 1)
? "The array length is zero"
: "The array length is " + String.valueOf(arrayName.length)
);
回答by Dan King
I would consider using ArrayUtils.is empty by adding Apache Commons Lang from here http://commons.apache.org/proper/commons-lang/download_lang.cgi
我会考虑通过从这里添加 Apache Commons Lang 来使用 ArrayUtils.is 空http://commons.apache.org/proper/commons-lang/download_lang.cgi
The big advantage is this will null check the array for you in a clean and easily readible way.
最大的优点是这将以干净且易于阅读的方式为您检查数组。
You can then do:
然后你可以这样做:
if (ArrayUtils.isEmpty(arrayName) {
System.out.printLn("Array empty");
} else {
System.out.printLn("Array not empty");
}