集册 Java实例教程 检查数组中是否包含项

检查数组中是否包含项

欢马劈雪     最近更新时间:2020-01-02 10:19:05

412
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
检查数组中是否包含项

/* 
 来自 
*时   代     Java  公  众  号 - nowjava.com*/

//package com.nowjava;


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));

    }


    public static boolean contains(int content, int[] nums) {

        boolean yes = false;

        for (int i : nums) {

            if (content == i)
            /* from 
            N o  w  J a v a . c o m - 时  代  Java*/

                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, 
展开阅读全文