Java 如何检查数组是否为空/空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2369967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:37:41  来源:igfitidea点击:

How can I check whether an array is null / empty?

javaarrays

提问by Ankit Sachan

I have an intarray which has no elements and I'm trying to check whether it's empty.

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

For example, why is the condition of the if-statement in the code below never true?

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

int[] k = new int[3];

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

采纳答案by cletus

There's a key difference between a nullarray and an empty array. This is a test for null.

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

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

"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:

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

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

An alternative definition of "empty" is if all the elements are null:

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

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

or

或者

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

回答by unwind

An intarray without elements is not necessarily null. It will only be nullif it hasn't been allocated yet. See this tutorialfor more information about Java arrays.

int没有元素的数组不一定是null。只有在null尚未分配的情况下才会如此。有关 Java 数组的更多信息,请参阅本教程

You can test the array's length:

您可以测试数组的长度:

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

回答by shahkalpesh

I am from .net background. However, java/c# are more/less same.

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

If you instantiate a non-primitive type (array in your case), it won't be null.
e.g. int[] numbers = new int[3];
In this case, the space is allocated & each of the element has a default value of 0.

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

It will be null, when you don't newit up.
e.g.

它会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");
}

回答by Mike

Look at its length:

看它的长度:

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

Though it's safer to check for null at the same time:

虽然同时检查 null 更安全:

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

回答by objects

An int array is initialised with zero so it won't actually ever contain nulls. Only arrays of Object's will contain null initially.

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

回答by vickirk

I believe that what you want is

我相信你想要的是

int[] k = new int[3];

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

You newed it up so it was never going to be null.

您对其进行了更新,因此它永远不会为空。

回答by Shravan Ramamurthy

ArrayUtils.isNotEmpty(testArrayName)from the package org.apache.commons.lang3ensures Array is not null or empty

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

回答by hubbabubba

The point here very simply is that the variable k isn't null because it points to the array. It doesn't matter that the array itself is empty. The null test in your post would only evaluate to true if the variable k didn't point to anything.

这里的要点很简单,变量 k 不为空,因为它指向数组。数组本身是否为空并不重要。如果变量 k 没有指向任何内容,则您帖子中的空测试只会评估为真。

回答by Uddhav Gautam

I tested as below. Hope it helps.

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

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");
        }   

回答by J_fruitty

You can also check whether there is any elements in the array by finding out its length, then put it into if-else statement to check whether it is null.

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

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