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

Java examples for Collection Framework:Array Contain

Description

Check if an int array contains an int value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int key = 2;
        System.out.println(contains(array, key));
    }/*www.j a va 2  s .c o  m*/

    public static boolean contains(int[] array, int key) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials