12

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!

tamir
  • 155
  • 1
  • 1
  • 10
  • 4
    That depends on what you mean by __empty__ . – Nabin Jan 18 '16 at 04:11
  • *"I wanted to know if this code is valid for checking whether an array is empty"* — the code you posted doesn't even compile. In the future, it's probably faster to just **try compiling your code** rather than asking people on the internet to look at it. – DaoWen Jan 18 '16 at 04:29

6 Answers6

12

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");
}
Dan King
  • 1,080
  • 1
  • 11
  • 28
9

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");
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
1

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");
    } 
Omkar Puttagunta
  • 4,036
  • 3
  • 22
  • 35
0

No, because array literals don't work that way. Replace arrayName={} with arrayName.length==0, and it should work.

ostrichofevil
  • 749
  • 7
  • 19
0

To check empty try like this:

if (arr.length == 0) {
 System.out.println("array is empty");
}
soorapadman
  • 4,451
  • 7
  • 35
  • 47
0

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)
);
scottb
  • 9,908
  • 3
  • 40
  • 56