繁体   English   中英

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

[英]How to check if an element in a string array is empty?

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

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(correct[0].length() > 0)给了我NPE。 if(correct[0] != null也。

您是否尝试过“显而易见的”:

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

只需将其与null进行比较,这是某些对象类型的数组的默认值。

if (correct[0] != null && correct[0].length() > 0)

&&运算符仅在左侧为true时才对右侧求true ,即correct[0]不为null且不会抛出NullPointerException

如果您要检查特定位置的特定元素是否为空(不仅不同于null ,而且也不同于"" ),那么提出的解决方案将不够。

Java有很多方法可以做到这一点。 让我们看看其中的一些:

  1. 您可以将数组转换为List (使用Arrays's asList方法),并通过contains()方法检查条件:

     import java.util.*; public class Test1 { private static boolean method1_UsingArrays(String[] array) { return Arrays.asList(array).contains(null) || Arrays.asList(array).contains(""); } } 
  2. 也可以使用Collections's disjoint方法来检查两个collection是否具有相同的元素:

     import java.util.*; public class Test2 { private static String[] nullAndEmpty = {"", null}; private static boolean method2_UsingCollectionsDisjoint(String[] array) { // disjoint returns true if the two specified collections have no elements in common. return !Collections.disjoint( // Arrays.asList(array), // Arrays.asList(nullAndEmpty) // ); } } 
  3. 如果您能够使用Java 8 (我最喜欢的选项),则可以使用新的Streams API

     import java.util.stream.*; import java.util.*; public class Test3 { private static boolean method3_UsingJava8Streams(String[] array) { return Stream.of(array).anyMatch(x -> x == null || "".equals(x)); } } 

    与其他选项相比,如果您需要trim每个字符串(或调用任何其他String的方法),这甚至更容易处理:

     Stream.of(array).anyMatch(x -> x == null || "".equals(x.trim())); 

然后,您可以轻松地调用和测试它们:

public static void main(String[] args) {
  // Ok tests
  String[] ok = {"a", "b", "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(ok)); // false
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(ok)); // false
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(ok)); // false

  System.out.println("-------------------------------");

  // Nok tests
  String[] nok = {"a", null, "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(nok)); // true
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(nok)); // true
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(nok)); // true

  // Nok tests
  String[] nok2 = {"a", "", "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(nok2)); // true
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(nok2)); // true
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(nok2)); // true
}

好吧,如果您不能使用isEmpty尝试使用Array.Contains(null)或尝试这个小循环

for(int i = 0;i<=Array.length;i++){
  if(array[i] > 0 && array[i] != null){
     //yay its not null
  }
}

有类似的版本,其中参数数量可能更大

if (Arrays.asList(new String[] {productId, productTitle, imageName, productPrice}).contains(null)) {
        return null;
}

只需使用isAnyEmpty

org.apache.commons.lang3
StringUtils.isAnyEmpty(final CharSequence... css)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM