检查java中字符串数组的大小或长度

ath*_*esh 3 java

我有这个

String p[] = null;
Run Code Online (Sandbox Code Playgroud)

我需要检查它的大小/长度是否null合适.就像是

if (p.length == null)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

Bal*_*usC 8

你不能检查长度null.此外,length返回一个int永远不会的null.做就是了

if (p == null) {
    // There is no array at all.
} else if (p.length == 0) {
    // There is an array, but there are no elements.
}
Run Code Online (Sandbox Code Playgroud)

或者只是实例化它而不是保留它null.

String[] p = new String[0];
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

if (p.length == 0) {
    // There are no elements.
}   
Run Code Online (Sandbox Code Playgroud)

也可以看看: