Java 将 Long 转换为 Enum 类型问题
我在将 Java long 类型转换为 Enum 类型时遇到了一个小问题,并且找不到解决方案。
这是我正在使用的:
public enum DataType {
IMAGES(1),
VIDEOS(2);
private int value;
private DataType(int i){
this.value = i;
}
}
我需要做这样的事情:
DataType dataType;
String thiz = "1";
long numb = Long.parseLong(thiz);
dataType = numb;
我得到的错误说:
将 numb 转换为 DataType 或将 dataType 转换为 long。
第二种情况:
我有这样的情况:
static String[] packetType;
String tmp=incomingData.toString(); // where incomingData is byte[]
int lastLoc = 0;
int needsSize = packetFieldSizes[tmpCurrentField-1]; // where packetFieldSizes,tmpCurrentField are integers.
thiz=tmp.substring(lastLoc, needsSize);
packetType=thiz; // packetType = thiz copy; where thiz is the same as used above.
我尝试将 thiz 转换为 String[] 并使用 valueOf,但是
有任何建议如何获取认为工作?
提前致谢!
I had a little problem with casting Java long type to Enum type and can't find a solution how to do that.
Here is what I'm using :
public enum DataType {
IMAGES(1),
VIDEOS(2);
private int value;
private DataType(int i){
this.value = i;
}
}
and I need to do something like this:
DataType dataType;
String thiz = "1";
long numb = Long.parseLong(thiz);
dataType = numb;
The error that I get says:
Convert numb to DataType or convert dataType to long.
Second Scenario:
I have this :
static String[] packetType;
String tmp=incomingData.toString(); // where incomingData is byte[]
int lastLoc = 0;
int needsSize = packetFieldSizes[tmpCurrentField-1]; // where packetFieldSizes,tmpCurrentField are integers.
thiz=tmp.substring(lastLoc, needsSize);
packetType=thiz; // packetType = thiz copy; where thiz is the same as used above.
I tried to convert thiz to String[] and use valueOf,but
Any suggestions how to get the thinks to work?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果传递给枚举的数字是索引而不是某些任意代码(例如分辨率或行中的字符数),ordinal() 将起作用。
我使用值的访问方法、解析索引和将数字解析为枚举值的静态方法来扩展枚举。给你...
请注意,此解决方案将不起作用,因为您的地图枚举值不是双射的,因此您的枚举类型中的枚举可能只有不同的值。
ordinal() will work if the numbers you are passing to the enum are indexes and not some arbitrary code, like resolution or number of chars in a line.
I'd extends the enum with an accesor method for the value, a resolution index and a static method that resolves a number into a Enum value. Here you go...
Note that this solution won't work is your map Enum-value is not bijective, so you may only have distinct values for the enums in your enumType.
Enum
已经为其每个实例提供了一个唯一的整数。查看ordinal ()
。 (请注意,它是从零开始的。)如果您需要从
long
转换为DataType
,您可以执行枚举常量、字符串之间的转换的完整列表整数可以在这个答案中找到:
Enum
already provides a unique integer for each of it's instances. Check outordinal()
. (Note that it's zero-based though.)If you need to go from a
long
to aDataType
you can doA complete list of conversions from and to enum constants, strings and integers can be found in this answer:
如果由于某种原因您需要自己分配数字,从而无法使用 aioobe 的良好解决方案,您可以执行如下操作:
静态方法
getByValue()
搜索DataType< /code> 与提供的号码。
If for some reason you need to assign the numbers yourself and thereby can't use aioobe's good solution, you can do something like the following:
The static method
getByValue()
searches for theDataType
with the provided number.除了@aioobe的答案之外,您还可以滚动您自己的
getInstance
方法。这将提供更大的灵活性,因为您不会依赖于序数。In addition to @aioobe's answer, you could roll your own
getInstance
method. This would provide more flexibility, since you wouldn't be dependent on the ordinal.aioobe 的正确答案。也许还有其他一些担忧?
对于枚举索引,最好使用 int 而不是 long。
它可能是这样的:
请注意“-1”,因为数组是从零开始的,而索引是从一开始的。
Correct answer from aioobe. Maybe some other concerns ?
You could be better of using int instead of long, for the enum index.
It could be like :
Please note the "-1", as arrays are zero-based while your index is one-based.