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

后端 未结 29 2859
予麋鹿
予麋鹿 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:33

    One possible solution:

    import java.util.Arrays;
    import java.util.List;
    
    public class ArrayContainsElement {
      public static final List VALUES = Arrays.asList("AB", "BC", "CD", "AE");
    
      public static void main(String args[]) {
    
          if (VALUES.contains("AB")) {
              System.out.println("Contains");
          } else {
              System.out.println("Not contains");
          }
      }
    }
    

提交回复
热议问题