check if array is empty java code example

Example 1: how to check if a list is empty java

if (list != null && !list.isEmpty()) { do something }

Example 2: check if array is empty java

if (myArray == null || myArray.length == 0) { }

Example 3: isempty for arrays

Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
  if (ob != null) {
    empty = false;
    break;
  }
}

Example 4: java check if array element is null

public class test {

    public static void main(String[] args) {
        Object[][] someArray = new Object[5][];
        someArray[0] = new Object[10];
        someArray[1] = null;
        someArray[2] = new Object[1];
        someArray[3] = null;
        someArray[4] = new Object[5];

        for (int i=0; i<=someArray.length-1; i++) {
            if (someArray[i] != null) {
                System.out.println("not null");
            } else {
                System.out.println("null");
            }
        }
    }
}

$ /cygdrive/c/Program\ Files/Java/jdk1.6.0_03/bin/java -cp . test
not null
null
not null
null
not null

Example 5: isempty for arrays

arr = new int[0];
if (arr.length == 0) {
  System.out.println("array is empty");
}

Example 6: isempty for arrays

Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    empty = false;
    break;
  }
}

Tags:

Php Example