How do I determine whether an array contains a particular value in Java?

后端 未结 29 2747
予麋鹿
予麋鹿 2020-11-21 05:00

I have a String[] with values like so:

public static final String[] VALUES = new String[] {\"AB\",\"BC\",\"CD\",\"AE\"};

Given

29条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 05:54

    You can check it by two methods

    A)By converting the array into string and then check the required string by .contains method

     String a=Arrays.toString(VALUES);
        System.out.println(a.contains("AB"));
        System.out.println(a.contains("BC"));
        System.out.println(a.contains("CD"));
        System.out.println(a.contains("AE"));
    

    B)this is a more efficent method

     Scanner s=new Scanner(System.in);
    
    
       String u=s.next();
       boolean d=true;
        for(int i=0;i

提交回复
热议问题