Check if an Item is contained in an array - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Check if an Item is contained in an array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int content = 2;
        int[] nums = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(contains(content, nums));
    }//ww w  .  j  av  a2 s  .  c o m

    public static boolean contains(int content, int[] nums) {
        boolean yes = false;
        for (int i : nums) {
            if (content == i)
                yes = true;
        }
        return yes;
    }

    public static boolean contains(String content, String[] nums) {
        boolean yes = false;
        for (String i : nums) {
            if (content.equals(i))
                yes = true;
        }
        return yes;
    }

    public static boolean contains(char content, char[] nums) {
        boolean yes = false;
        for (char i : nums) {
            if (content == i)
                yes = true;
        }
        return yes;
    }
}

Related Tutorials