check if an array contains an int value - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

check if an array contains an int value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object[] myObj = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        int indexNum = 2;
        System.out.println(anyEqual(myObj, indexNum));
    }//from  ww w .j a v  a  2  s. c  om

    public static boolean anyEqual(Object[] myObj, int indexNum) {
        for (int a = 0; a < indexNum; a++) {
            for (int b = 0; b < indexNum; b++) {
                if ((a != b) && (myObj[a] == myObj[b])) {
                    return true;
                }
            }
        }
        return false;
    }
}

Related Tutorials