Java 如何检查数组是否为null/空?
我有一个Java 如何检查数组是否为null/空?,java,arrays,Java,Arrays,我有一个int数组,它没有元素,我正在检查它是否为空 例如,为什么下面代码中if语句的条件永远不为真 int[] k = new int[3]; if (k == null) { System.out.println(k.length); } null数组和空数组之间有一个关键区别。这是对null的测试 int arr[] = null; if (arr == null) { System.out.println("array is null"); } 这里的“空”没有正式意义
int
数组,它没有元素,我正在检查它是否为空
例如,为什么下面代码中if语句的条件永远不为真
int[] k = new int[3];
if (k == null) {
System.out.println(k.length);
}
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");
}
“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;
}
}
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");
}
“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;
}
}
没有元素的
int
数组不一定是null
。只有在尚未分配的情况下,它才会null
。有关Java阵列的更多信息,请参阅
您可以测试阵列的长度:
void foo(int[] data)
{
if(data.length == 0)
return;
}
没有元素的
int
数组不一定是null
。只有在尚未分配的情况下,它才会null
。有关Java阵列的更多信息,请参阅
您可以测试阵列的长度:
void foo(int[] data)
{
if(data.length == 0)
return;
}
我来自.net背景。然而,java/c#基本相同 如果实例化一个非基元类型(在您的例子中是数组),它将不会为null。
e、 g.
int[]数字=新的int[3]代码>
在这种情况下,将分配空间&每个元素的默认值为0
如果您不new
将其设置为null
。
e、 g
我来自.net背景。然而,java/c#基本相同
如果实例化一个非基元类型(在您的例子中是数组),它将不会为null。
e、 g.int[]数字=新的int[3]代码>
在这种情况下,将分配空间&每个元素的默认值为0
如果您不new
将其设置为null
。
e、 g
看看它的长度:
int[] i = ...;
if (i.length == 0) { } // no elements in the array
尽管同时检查null更安全:
if (i == null || i.length == 0) { }
看看它的长度:
int[] i = ...;
if (i.length == 0) { } // no elements in the array
尽管同时检查null更安全:
if (i == null || i.length == 0) { }
int数组是用零初始化的,因此它实际上不包含null。只有对象的数组最初才包含null。int数组是用零初始化的,因此它实际上永远不会包含null。最初只有对象的数组才会包含null。我相信您想要的是
int[] k = new int[3];
if (k != null) { // Note, != and not == as above
System.out.println(k.length);
}
你更新了它,所以它永远不会为空。我相信你想要的是
int[] k = new int[3];
if (k != null) { // Note, != and not == as above
System.out.println(k.length);
}
您更新了它,这样它就永远不会为空。ArrayUtils.isNotEmpty(testArrayName)
从包org.apache.commons.lang3
确保数组不为空。ArrayUtils.isNotEmpty(testArrayName)
来自包org.apache.commons.lang3
确保数组不为null或空这里非常简单的一点是变量k不为null,因为它指向数组。数组本身是否为空并不重要。只有当变量k没有指向任何东西时,帖子中的null测试才会计算为true。这里的要点很简单,就是变量k不是null,因为它指向数组。数组本身是否为空并不重要。只有当变量k没有指向任何东西时,您文章中的空测试才会计算为true。我测试如下。希望能有帮助
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");
}
我测试如下。希望能有帮助
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");
}
您还可以通过找出数组的长度来检查数组中是否有元素,然后将其放入if-else语句中检查它是否为null
int[] k = new int[3];
if(k.length == 0)
{
//do something
}
您还可以通过找出数组的长度来检查数组中是否有元素,然后将其放入if-else语句中检查它是否为null
int[] k = new int[3];
if(k.length == 0)
{
//do something
}
public boolean empty(){
布尔值isEmpty=true;
int i=0;
对于(int j=0;j
这是我最接近检查int数组是否为空的地方。
虽然当数组中的整数实际上为零时,这将不起作用。它适用于{1,2,3},如果{2,0},它仍将返回false,{0}将返回truepublic boolean empty(){
布尔值isEmpty=true;
int i=0;
对于(int j=0;j
这是我最接近检查int数组是否为空的地方。
虽然当数组中的整数实际上为零时,这将不起作用。它适用于{1,2,3},如果{2,0},它仍将返回false,{0}将返回true检查数组是否为null或空的方法也存在于org.apache.commons.lang上:
import org.apache.commons.lang.ArrayUtils;
ArrayUtils.isEmpty(array);
org.apache.commons.lang上还提供了检查数组是否为null或空的方法:
import org.apache.commons.lang.ArrayUtils;
ArrayUtils.isEmpty(array);
在Java8+中,您可以通过streams-allMatch方法实现这一点
对于原语:
int[] k = new int[3];
Arrays.stream(k).allMatch(element -> element != 0)
对于对象:
Objects[] k = new Objects[3];
Arrays.stream(k).allMatch(Objects::nonNull)
在Java8+中,您可以通过streams-allMatch方法实现这一点
对于原语:
int[] k = new int[3];
Arrays.stream(k).allMatch(element -> element != 0)
对于对象:
Objects[] k = new Objects[3];
Arrays.stream(k).allMatch(Objects::nonNull)
如果您试图在spring框架中检查,那么objectUtils类中的isEmpty方法会有所帮助
public static boolean isEmpty(@Nullable Object[] array) {
return (array == null || array.length == 0);
}
如果您试图在spring框架中检查,那么objectUtils类中的isEmpty方法会有所帮助
public static boolean isEmpty(@Nullable Object[] array) {
return (array == null || array.length == 0);
}
你能多发一点代码吗?数组初始化的位置将非常有用。我不确定您的要求是什么。当然,如果要检查数组是否为空,我们会说(array==null)如果(k!=null)您不想要吗?请您再发布一点代码好吗?数组初始化的位置将非常有用。我不确定您的要求是什么。如果要检查数组是否为空,肯定会说