Java: Check if enum contains a given string?

后端 未结 29 1579
一个人的身影
一个人的身影 2020-12-02 03:59

Here\'s my problem - I\'m looking for (if it even exists) the enum equivalent of ArrayList.contains();.

Here\'s a sample of my code problem:

<         


        
29条回答
  •  星月不相逢
    2020-12-02 05:02

    enum are pretty powerful in Java. You could easily add a contains method to your enum (as you would add a method to a class):

    enum choices {
      a1, a2, b1, b2;
    
      public boolean contains(String s)
      {
          if (s.equals("a1") || s.equals("a2") || s.equals("b1") || s.equals("b2")) 
             return true;
          return false;
      } 
    
    };
    

提交回复
热议问题