集册 Java实例教程 使用for循环检查数组是否包含值

使用for循环检查数组是否包含值

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

460
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用for循环检查数组是否包含值


import java.util.Arrays;
/**
n o w  j a v a  . c o m
**/


public class Main {


  public static void main(String args[]) {


    String[] strMonths = new String[] { "January", "February", "March", "April", "May" };


    String strFind1 = "March";

    String strFind2 = "December";
/* from n o w j a   v  a . c o m - 时  代  Java*/

    boolean contains = false;


    for (int i = 0; i < strMonths.length; i++) {

      if (strMonths[i].equals(strFind1)) {

        contains = true;

        break;

      }

    }


    if (contains) {

      System.out.println("String array contains String " + strFind1);

    } else {

      System.out.println("String array does not contain String " + strFind1);

    }


    contains = Arrays.asList(strMonths).contains(strFind1);

    System.out.println("Does String array contain " + s
展开阅读全文