如何检查字符串数组中的元素是否为空?

Jak*_*ała 0 java

如何检查字符串数组中的元素是否为空?这是我的示例数组:

private static String correct[] = new String[5];
    static {
        correct[1] = "some texT";
        correct[2] = "some texT3";
        correct[4] = "some texT2";
}
Run Code Online (Sandbox Code Playgroud)

我可以分配null其余的元素,但我想找到另一个更好的方法来做到这一点.我找到了isEmpty,但它仅适用于API 9及更高版本.

if(correct[0].length() > 0) 给我一个NPE. if(correct[0] != null也.

rol*_*lfl 5

你尝试过"明显的":

if(correct[0] != null && correct[0].length() > 0) {
   //it is not null and it is not empty
}
Run Code Online (Sandbox Code Playgroud)