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!

asked Jan 18, 2016 at 4:09
2
  • 4
    That depends on what you mean by empty . Commented Jan 18, 2016 at 4: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. Commented Jan 18, 2016 at 4:29

6 Answers 6

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");
}
answered Jan 20, 2016 at 5:40

Comments

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");
answered Jan 18, 2016 at 4:14

Comments

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");
 } 
answered Jan 18, 2016 at 4:54

Comments

0

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

answered Jan 18, 2016 at 4:11

Comments

0

To check empty try like this:

if (arr.length == 0) {
 System.out.println("array is empty");
}
answered Jan 18, 2016 at 4:12

1 Comment

This is a duplicate of the other answer.
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)
);
answered Jan 18, 2016 at 5:12

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.