Get index of enum from string?

前端 未结 5 1373
暗喜
暗喜 2021-01-07 17:55

I have a string value, I also have an array of strings and an enum containing the range also. To get the index of the string in the array, from the value supplied I write th

5条回答
  •  粉色の甜心
    2021-01-07 18:12

    If you want to retrieve the index, you can use ordinal. If you want to assign some specific value to String, you may define your own method to retrieve it.

     enum DAY
     {
      MONDAY(10),
      TUESDAY(20);
      int value;
      DAY(int x)
      {
       this.value = x;
      }
      public int getValue()
      {
        return value;
       }
    

    Now value and ordinal can be retrieved as :

        for(DAY s : DAY.values() )
        {
            System.out.println(s.ordinal());
            System.out.println(s.getValue());
        }
    

提交回复
热议问题