如何检查字符串数组中的元素是否为空?这是我的示例数组:
private static String correct[] = new String[5];
static {
correct[1] = "some texT";
correct[2] = "some texT3";
correct[4] = "some texT2";
}
我可以将 null
分配给其余元素,但我想找到另一种更好的方法来执行此操作。
我找到了 isEmpty
,但它仅在 API 9 及更高版本上可用。
if(正确[0].length() > 0)
给我一个 NPE。
if(正确[0] != null
也。
最佳答案
你尝试过“显而易见的”吗:
if(correct[0] != null && correct[0].length() > 0) {
//it is not null and it is not empty
}
关于java - 如何检查字符串数组中的元素是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19801152/