【问题标题】:How can I check whether an array is null / empty?如何检查数组是否为空/空?
【发布时间】:2011-01-23 02:54:00
【问题描述】:

我有一个没有元素的 int 数组,我正在尝试检查它是否为空。

例如,为什么下面代码中的 if 语句的条件永远不会为真?

int[] k = new int[3];

if (k == null) {
    System.out.println(k.length);
}

【问题讨论】:

  • 你能多贴一些代码吗?初始化数组的位会很有用。
  • 我不知道你在问什么。肯定要检查一个数组是否为空,有人会说 (array == null)
  • 你不想要 if (k != null)

标签: java arrays


【解决方案1】:

null 数组和空数组之间有一个关键区别。这是对null 的测试。

int arr[] = null;
if (arr == null) {
  System.out.println("array is null");
}

这里的“空”没有官方含义。我选择将空定义为具有 0 个元素:

arr = new int[0];
if (arr.length == 0) {
  System.out.println("array is empty");
}

“空”的另一种定义是如果所有元素都是null

Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    empty = false;
    break;
  }
}

Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
  if (ob != null) {
    empty = false;
    break;
  }
}

【讨论】:

  • ups,最后一个sn-p有obj !- null,应该是obj != null
  • 别忘了:org.apache.commons.lang3.ArrayUtils.isEmpty(k)
  • 记住,=== 是行不通的。您必须使用 ==,因为 null 属于不同类型。
【解决方案2】:

ArrayUtils.isNotEmpty(testArrayName) 来自包 org.apache.commons.lang3 确保 Array 不为 null 或为空

【讨论】:

    【解决方案3】:

    看它的长度:

    int[] i = ...;
    if (i.length == 0) { } // no elements in the array
    

    虽然同时检查 null 更安全:

    if (i == null || i.length == 0) { }
    

    【讨论】:

    • 如果您需要同时检查 null 和长度 0,请务必先检查 null,以避免可能出现的空指针错误
    【解决方案4】:

    org.apache.commons.lang 上也有检查数组是否为空或空的方法:

    import org.apache.commons.lang.ArrayUtils;
    
    ArrayUtils.isEmpty(array);
    

    【讨论】:

      【解决方案5】:

      我来自 .net 背景。但是,java/c# 或多或少是相同的。

      如果你实例化一个非原始类型(在你的例子中是数组),它就不会为空。
      例如int[] numbers = new int[3];
      在这种情况下,分配了空间,并且每个元素都有一个默认值 0。

      它会是null,当你不new它时。
      例如

      int[] numbers = null; // changed as per @Joachim's suggestion.
      if (numbers == null)
      {
         System.out.println("yes, it is null. Please new it up");
      }
      

      【讨论】:

      • 在Java中不会编译,因为它会告诉你numbers还没有被初始化。 “未初始化”和null 不是一回事。
      • 谢谢约阿希姆。我将编辑帖子以将int[] numbers 更改为int[] numbers == null;在 c# 中,情况并非如此。
      【解决方案6】:

      一个 int 数组初始化为零,因此它实际上不会包含空值。只有 Object 的数组最初会包含 null。

      【讨论】:

      • 如果我必须检查 null 是否为整数
      • 不能用 int 等原语检查 null。
      • 取决于您声明它的位置,如果作为类成员,那么是的,它会被初始化为零。但是当在方法中本地声明时,我相信这是另一种情况......你必须自己分配一个初始值。我想。只是一个想法!
      【解决方案7】:

      这里的要点很简单,就是变量 k 不为空,因为它指向数组。数组本身是否为空并不重要。只有当变量 k 没有指向任何东西时,您帖子中的 null 测试才会评估为 true。

      【讨论】:

        【解决方案8】:

        我测试如下。希望能帮助到你。

        Integer[] integers1 = new Integer[10];
                System.out.println(integers1.length); //it has length 10 but it is empty. It is not null array
                for (Integer integer : integers1) {
                    System.out.println(integer); //prints all 0s
                }
        
        //But if I manually add 0 to any index, now even though array has all 0s elements
        //still it is not empty
        //        integers1[2] = 0;
                for (Integer integer : integers1) {
                    System.out.println(integer); //Still it prints all 0s but it is not empty
                    //but that manually added 0 is different
                }
        
        //Even we manually add 0, still we need to treat it as null. This is semantic logic.
        
                Integer[] integers2 = new Integer[20];
                integers2 = null; //array is nullified
        //        integers2[3] = null; //If I had int[] -- because it is priitive -- then I can't write this line. 
                if (integers2 == null) {
                    System.out.println("null Array");
                }   
        

        【讨论】:

          【解决方案9】:

          在 Java 8+ 中,您可以借助流 allMatch 方法来实现这一点。

          对于原语:

          int[] k = new int[3];
          Arrays.stream(k).allMatch(element -> element != 0)
          

          对于对象:

          Objects[] k = new Objects[3];
          Arrays.stream(k).allMatch(Objects::nonNull)
          

          【讨论】:

            【解决方案10】:

            如果您尝试在 spring 框架中检查,那么 isEmpty(Object[]) 类中的 isEmpty(Object[]) 方法会有所帮助,

            public static boolean isEmpty(@Nullable Object[] array) {
                    return (array == null || array.length == 0);
                }
            

            【讨论】:

              【解决方案11】:

              没有元素的int 数组不一定是null。如果它还没有被分配,它只会是null。有关 Java 数组的更多信息,请参阅this tutorial

              你可以测试数组的长度:

              void foo(int[] data)
              {
                if(data.length == 0)
                  return;
              }
              

              【讨论】:

                【解决方案12】:
                    public boolean empty() {
                    boolean isEmpty = true;
                    int i = 0;
                    for (int j = 0; j < array.length; j++) {
                        if (array[j] != 0) {
                            i++;
                        }
                    }
                    if (i != 0) {
                        isEmpty = false;
                    }
                    return isEmpty;
                }
                

                这与我要检查一个 int 数组是否为空一样接近。 尽管当数组中的整数实际上为零时,这将不起作用。它适用于 {1,2,3},如果 {2,0} 仍会返回 false,但 {0} 将返回 true

                【讨论】:

                  【解决方案13】:

                  我相信你想要的是

                  int[] k = new int[3];
                  
                  if (k != null) {  // Note, != and not == as above
                      System.out.println(k.length);
                  }
                  

                  你更新了它,所以它永远不会为空。

                  【讨论】:

                    【解决方案14】:

                    也可以通过查找数组的长度来判断数组中是否有元素,然后放入if-else语句中判断是否为null。

                    int[] k = new int[3];
                    if(k.length == 0)
                    {
                    //do something
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-11-04
                      • 2012-07-02
                      • 1970-01-01
                      • 2017-09-13
                      • 2019-09-05
                      相关资源
                      最近更新 更多