从整数值(序数)获取枚举

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从整数值(序数)获取枚举相关的知识,希望对你有一定的参考价值。

A simple way to get the enum indicated by its ordinal value.
  1. public class EnumTester {
  2.  
  3. enum Color { RED, GREEN, BLUE };
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int selection = 1;
  8. Color selectedColor = Color.values()[ selection ];
  9.  
  10. System.out.println("Selected color ="+selectedColor);
  11. }
  12. }

以上是关于从整数值(序数)获取枚举的主要内容,如果未能解决你的问题,请参考以下文章