如何将int与枚举匹配

2022-01-10 00:00:00 enums android-ndk android java

我正在从 Android 中的本机代码接收 long 或 int 形式的返回值,我想将其转换或与 enum 匹配,以进行处理.是否可以 ?怎么样?

I am receiving return value in the form of long or int from Native code in Android, which I want to convert or match with enum, for processing purpose. Is it possible ? How?

推荐答案

如果你可以完全控制值和枚举,并且它们是顺序的,你可以使用枚举序数值:

If you have full control of values and enums, and they're sequential, you can use the enum ordinal value:

enum Heyo
{
  FirstVal, SecondVal
}

...later

int systemVal = [whatever];
Heyo enumVal = Heyo.values()[systemVal];

int againSystemVal = enumVal.ordinal();

相关文章