I have int
array which has no elements and I'm trying to check whether it's empty.
我有一个没有元素的int数组我正在检查它是否为空。
For example, why is the if-statement in the below code never true?
例如,为什么下面代码中的if语句从不为真?
int[] k = new int[3];if(k==null){ System.out.println(k.length);}
11 个解决方案
#1
156
There's a key difference between a null
array and an empty array. This is a test for 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:
“空”在这里没有官方意义。我选择将empty定义为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
:
“空”的另一定义是如果所有元素都为空:
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; }}
#2
39
ArrayUtils.isNotEmpty(testArrayName)
from the package org.apache.commons.lang3
ensures Array is not null or empty
isnotempty (testArrayName)来自包org.apache.common .lang3确保数组不是空的
#3
14
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:
虽然在同一时间检查空值更安全:
if (i == null || i.length == 0) { }
#4
2
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.
如果实例化一个非基元类型(在您的例子中是数组),它就不是null。例如int[] numbers = new int[3];在这种情况下,分配空间&每个元素的默认值为0。
It will be null
, when you don't new
it up.
e.g.
它将是空的,当你不更新它。如。
int[] numbers = null; // changed as per @Joachim's suggestion.if (numbers == null){ System.out.println("yes, it is null. Please new it up");}
#5
1
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数组初始化为零,因此它实际上不会包含null。只有对象的数组最初将包含null。
#6
1
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没有指向任何东西时,post中的零检验才会为真。
#7
1
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"); }
#8
0
An int
array without elements is not necessarily null
. It will only be null
if it hasn't been allocated yet. See this tutorial for more information about Java arrays.
没有元素的int数组不一定是空的。它只有在尚未分配时才为空。有关Java数组的更多信息,请参见本教程。
You can test the array's length:
您可以测试数组的长度:
void foo(int[] data){ if(data.length == 0) return;}
#9
0
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语句中,以检查它是否为null。
int[] k = new int[3];if(k.length == 0){//do something}
#10
0
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;}
This is as close as I got to checking if an int array is empty.Although this will not work when the ints in the array are actually zero. It'll work for {1,2,3}, and it'll still return false if {2,0} but {0} will return true
这就像我检查一个int数组是否为空一样。虽然当数组中的ints实际上为零时,这将不起作用。它对{1,2,3}有效,如果{2,0}它仍然返回false,但是{0}将返回true
#11
-1
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.
你把它更新了,所以它永远不会是零。
#1
156
There's a key difference between a null
array and an empty array. This is a test for 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:
“空”在这里没有官方意义。我选择将empty定义为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
:
“空”的另一定义是如果所有元素都为空:
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; }}
#2
39
ArrayUtils.isNotEmpty(testArrayName)
from the package org.apache.commons.lang3
ensures Array is not null or empty
isnotempty (testArrayName)来自包org.apache.common .lang3确保数组不是空的
#3
14
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:
虽然在同一时间检查空值更安全:
if (i == null || i.length == 0) { }
#4
2
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.
如果实例化一个非基元类型(在您的例子中是数组),它就不是null。例如int[] numbers = new int[3];在这种情况下,分配空间&每个元素的默认值为0。
It will be null
, when you don't new
it up.
e.g.
它将是空的,当你不更新它。如。
int[] numbers = null; // changed as per @Joachim's suggestion.if (numbers == null){ System.out.println("yes, it is null. Please new it up");}
#5
1
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数组初始化为零,因此它实际上不会包含null。只有对象的数组最初将包含null。
#6
1
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没有指向任何东西时,post中的零检验才会为真。
#7
1
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"); }
#8
0
An int
array without elements is not necessarily null
. It will only be null
if it hasn't been allocated yet. See this tutorial for more information about Java arrays.
没有元素的int数组不一定是空的。它只有在尚未分配时才为空。有关Java数组的更多信息,请参见本教程。
You can test the array's length:
您可以测试数组的长度:
void foo(int[] data){ if(data.length == 0) return;}
#9
0
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语句中,以检查它是否为null。
int[] k = new int[3];if(k.length == 0){//do something}
#10
0
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;}
This is as close as I got to checking if an int array is empty.Although this will not work when the ints in the array are actually zero. It'll work for {1,2,3}, and it'll still return false if {2,0} but {0} will return true
这就像我检查一个int数组是否为空一样。虽然当数组中的ints实际上为零时,这将不起作用。它对{1,2,3}有效,如果{2,0}它仍然返回false,但是{0}将返回true
#11
-1
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.
你把它更新了,所以它永远不会是零。