I wanted to know if this code is valid for checking whether an array is empty, or should I check for null?
if(arrayName={})
System.out.println("array empty");
else System.out.println("array not empty");
Thank you!
I wanted to know if this code is valid for checking whether an array is empty, or should I check for null?
if(arrayName={})
System.out.println("array empty");
else System.out.println("array not empty");
Thank you!
I would consider using ArrayUtils.is empty by adding Apache Commons Lang from here http://commons.apache.org/proper/commons-lang/download_lang.cgi
The big advantage is this will null check the array for you in a clean and easily readible way.
You can then do:
if (ArrayUtils.isEmpty(arrayName) {
System.out.printLn("Array empty");
} else {
System.out.printLn("Array not empty");
}
In array class we have a static variable defined "length", which holds the number of elements in array object. You can use that to find the length as:
if(arrayName.length == 0)
System.out.println("array empty");
else
System.out.println("array not empty");
As poited out in the other answers, the length
property of Array
will give the length of the array. But it is always recommended to check if the array is null
before doing any operation on it or else it will throw NullPointerException
if the array is null
.
if (array != null) {
if (array.length == 0)
System.out.println("Empty Array Size=0");
else
System.out.println("Array Not Empty - Size = " + array.length);
} else
System.out.println("array is null");
}
No, because array literals don't work that way. Replace arrayName={}
with arrayName.length==0
, and it should work.
To check empty try like this:
if (arr.length == 0) {
System.out.println("array is empty");
}
proudandhonour's answer is on the right track but won't work for all possible values of arrayName
, specifically that case in which arrayName
hasn't been defined and is null
. In that case, the code:
if(arrayName.length == 0)
System.out.println("array empty");
else
System.out.println("array not empty");
will fail with a NullPointerException.
TimeTravel's answer correctly tests for this case and correctly handles all possible values for arrayName
. The only downside is that his code is more verbose than it needs to be.
Java provides short circuit evaluation of Boolean expressions. Specifically the result of (false && xx)
is false
for all possible values of xx
. Therefore when evaluating the first operand of a Boolean &&
operator, the JVM will ignore the 2nd operand if the 1st evaluates to false.
Exploiting this, we can write:
if (arrayName != null && arrayName.length > 0)
{ System.out.println("The array length > 0"); }
else
{ System.out.println("The array is null or empty"); }
There is also the ternary operator which provides a mechanism for inlining if-then-else expressions. This can improve readability in some circumstances:
System.out.println((arrayName == null)
? "The arrayName variable is null"
: (arrayName.length < 1)
? "The array length is zero"
: "The array length is " + String.valueOf(arrayName.length)
);