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

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

    Instead of using the quick array initialisation syntax too, you could just initialise it as a List straight away in a similar manner using the Arrays.asList method, e.g.:

    public static final List STRINGS = Arrays.asList("firstString", "secondString" ...., "lastString");
    

    Then you can do (like above):

    STRINGS.contains("the string you want to find");
    

提交回复
热议问题