is Item Contained In Array - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

is Item Contained In Array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String object = "java2s.com";
        String[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(isOnList(object, array));
    }/*from   w ww. j  av  a2s .  c  om*/

    public static boolean isOnList(String object, String[] array) {
        try {
            for (int i = 0; i < array.length; i++) {
                if (object.equals(array[i])) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Objects aren't same type!");
        }
        return false;
    }
}

Related Tutorials