Check if an array Contains a value - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Check if an array Contains a value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        Object[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        Object needle = "java2s.com";
        System.out.println(arrayContains(array, needle));
    }//from   ww  w  .  jav a 2s  . co m

    public static boolean arrayContains(Object[] array, Object needle) {
        for (Object arrayItem : array) {
            if (arrayItem.equals(needle)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials