There are two situations when a Java array is empty:
- The array is null, the length of the array cannot be calculated at this time;
int[] array = null; //数组类型的空引用,不指向任何对象
- The array is not null, but there are no elements in the array; at this time, the length of the array is not 0;
int[] array= new int[0]; //数组引用一个长度为0的数组对象
Therefore, the judgment of the one-dimensional two-dimensional array is as follows:
Determine if a one-dimensional array is empty
if(array==null||array.length==0)
return 0;
The judgment of one-dimensional arrays can follow the two situations mentioned above.
Determine the two-dimensional array is empty
if((array==null||array.length==0)||(array.length==1&&array[0].length==0))
return 0;
To determine whether a two-dimensional array is empty in Java, there are three cases to determine:
-
Whether the first address of the two-dimensional array is empty, that is, array==null;
-
Whether the two-dimensional array is {}, that is, when array.length==0;
-
Whether the two-dimensional array is { {}}, ie array.length=1&&array[0].length==0;