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

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

    ObStupidAnswer (but I think there's a lesson in here somewhere):

    enum Values {
        AB, BC, CD, AE
    }
    
    try {
        Values.valueOf(s);
        return true;
    } catch (IllegalArgumentException exc) {
        return false;
    }
    

提交回复
热议问题