Check if Object array contains an Object - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Check if Object array contains an Object

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        Object toTest = "java2s.com";
        System.out.println(contains(array, toTest));
    }//w ww  .  j ava2  s . c  om

    public static boolean contains(Object[] array, Object toTest) {
        for (Object e : array) {
            if (e.equals(toTest))
                return true;
        }
        return false;
    }
}

Related Tutorials