Find first occurrence of null value in non sorted array. - Android java.lang

Android examples for java.lang:array

Description

Find first occurrence of null value in non sorted array.

Demo Code


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        Object[] src = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(indexOfNull(src));
    }//from  w  w w  .  java2s.com

    /**
     * Find first occurrence of null value in non sorted array.
     * 
     * @param src
     * @return
     */
    public static int indexOfNull(Object[] src) {
        for (int i = 0; i < src.length; ++i) {
            if (src[i] == null) {
                return i;
            }
        }
        return -1;
    }
}

Related Tutorials