8

I want to know the best practice for checking whether a string array is empty or not.

String[] name = {"a" , "b"};

if (name == null) {
}

Is this a good practice or there are more best codes for the same?

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
Piolo Opaw
  • 1,471
  • 2
  • 15
  • 21
  • 1
    What do you mean by "boolean value of the String[]?" What do you expect to be true, what do you expect to be false? – David Ehrmann Mar 04 '14 at 03:28
  • I think the concept of a "boolean value of String[]" simply doesn't apply in Java. It's not like JavaScript where things are either truthy or falsy. You can check to see whether a reference is null, or whether the elements of an array are null, but that's about all. – ktm5124 Mar 04 '14 at 03:31

4 Answers4

24

Normally you would want to do something like:

if (arr != null && arr.length > 0) { ... }

for non-empty array.

However, as you may suspect, someone have made utils for such kind of common action. For example, in Commons-lang, you can do something like:

if (ArrayUtils.isEmpty(arr)) {... }

if you do a static import for ArrayUtils.isEmpty, this line can be even shorter and looks nicer:

if (isEmpty(arr)) { ... }
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • org.apache.commons.lang.ArrayUtils this will be more useful IF THE PROJECT IS ALREADY USING **apache package** , normally simple prog dont use that jar distribution – Srinath Ganesh Mar 04 '14 at 06:42
  • @SrinathGanesh of course whether to use 3rd party lib is the choice of OP but, with modern build tools like Maven and Gradle, I don't see any problem using such lib in simple programs. – Adrian Shum Mar 04 '14 at 07:51
6
if(name!=null && name.length > 0) {
   // This means there are some elements inside name array.
} else {
   // There are no elements inside it.
}
Aditya
  • 1,334
  • 1
  • 12
  • 23
2

To check if a string array is empty...

public boolean isEmptyStringArray(String [] array){
 for(int i=0; i<array.length; i++){ 
  if(array[i]!=null){
   return false;
  }
  }
  return true;
}
Solace
  • 2,161
  • 1
  • 16
  • 33
2

All arrays in Java have a special field "length" that contains the number of elements in the array, that is array length in other words.

String test( String[] array )
{
    if ( array == null ) {
        return "array is null";
    }
    if ( array.length == 0 ) {
        return "array is empty, meaning it has no element";
    }
    for ( String s : array ) {
        if (s == null) {
            return "array contains null element";
        }
        if (s.length() == 0) {
            return "array contains empty string";
        }
        // or
        if (s.isEmpty()) {
            return "array contains empty string";
        }
    }

    return "array is not null or empty and does not contain null or empty strings";
}

To test whether the array contains a null element or an empty string you need to iterate through it and check each element individually.

Do not forget that the length of array is the special field array.legnth and the length of the string is the function string.length().

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52