How to check empty java array

I wanted to know if this code is valid for checking if the array is empty, or should I check for null?

if(arrayName={}) System.out.println("array empty"); else System.out.println("array not empty"); 

Thanks!

+6
source share
6 answers

In the array class, we have a static variable, defined by the "length", which contains the number of elements in the array object. You can use this to find the length as:

 if(arrayName.length == 0) System.out.println("array empty"); else System.out.println("array not empty"); 
+5
source

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 that it allows you to check the array for you in a clean and easy to read way.

Then you can:

 if (ArrayUtils.isEmpty(arrayName) { System.out.printLn("Array empty"); } else { System.out.printLn("Array not empty"); } 
+4
source

No, because array literals do not work. Replace arrayName={} with arrayName.length==0 , and it should work.

0
source

To check for empty, try like this:

 if (arr.length == 0) { System.out.println("array is empty"); } 
0
source

As shown in other answers, the length Array property will give the length of the array. But it is always recommended to check if the array was null before performing any operation on it, otherwise it will throw a 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"); } 
0
source

hotandhonour answer is on the right track, but will not work for all possible values ​​of arrayName , in particular, the case when arrayName not defined and null . In this case, the code:

 if(arrayName.length == 0) System.out.println("array empty"); else System.out.println("array not empty"); 

with a NullPointerException. error NullPointerException. TimeTravel will correctly check this case and correctly process all possible values ​​for arrayName . The only drawback is that its code is more verbose than it should be.

Java provides an assessment of the short circuits of Boolean expressions. In particular, the result (false && xx) is false for all possible xx values. Therefore, evaluating the first operand of the && logical operator, the JVM will ignore the second operand if the first evaluates to false.

Using 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 a ternary operator that provides a mechanism for building if-then-else expressions. This can improve readability in some cases:

 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) ); 
0
source

All Articles