Java Array Null Element isNull(final Object[] array)

Here you can find the source of isNull(final Object[] array)

Description

Checks if an array is null or empty or all its elements are null.

License

Open Source License

Parameter

Parameter Description
array a parameter

Return

true if array is null, empty or contains only null elements

Declaration

public static boolean isNull(final Object[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2013/*from   w  ww .  ja va 2 s.  c  o  m*/
 * Contributors: L. Armanet, M. Camerlenghi, L. Cardonne, S. Delageniere,
 *               L. Duparchy, S. Ohlsson, P. Pascal, I. Schneider, S.Schulze,
 *               F. Torres
 * 
 * This file is part of the MIS tools package.
 * 
 * The MIS tools package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The MIS tools package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the MIS tools package.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Checks if an array is null or empty or all its elements are null.
     * 
     * @param array
     * @return true if array is null, empty or contains only null elements
     */
    public static boolean isNull(final Object[] array) {
        if (array == null || array.length == 0) {
            return true;
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if a collection is null or empty or all its elements are null.
     * 
     * @param col
     * @return true if collection is null, empty or contains only null elements
     */
    public static boolean isNull(final Collection col) {
        if (col == null || col.isEmpty()) {
            return true;
        }
        Iterator it = col.iterator();
        while (it.hasNext()) {
            if (it.next() != null) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. isNull(Object[] objects)
  2. isNull(T array[])
  3. isNullArray(String[] array)