Get index of enum from string?

前端 未结 5 1378
暗喜
暗喜 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条回答
  •  Happy的楠姐
    2021-01-07 18:24

    The following logic will also work.

    If you want to check and you know the fruitname already don't use for loop go with the approach mentioned by Pshemo

    for (Fruit aFruit : aFruits)
               if (aFruit.name().equals(aName))
                   return aFruit.ordinal();
    

提交回复
热议问题