问题描述
我有枚举类型 ReportTypeEnum
,在所有类中的方法之间传递,但是我需要在URL上传递,所以我使用序数方法来获取int值。在我的其他JSP页面中,我需要将其转换回 ReportTypeEnum
,以便我可以继续传递。
I've the enum type ReportTypeEnum
that get passed between methods in all my classes but I then need to pass this on the URL so I use the ordinal method to get the int value. After I get it in my other JSP page, I need to convert it to back to an ReportTypeEnum
so that I can continue passing it.
如何将序数转换为 ReportTypeEnum
?
使用Java 6 SE。
Using Java 6 SE.
推荐答案
要将序数转换为其枚举代数,您可能需要这样做:
To convert an ordinal into its enum represantation you might want to do this:
ReportTypeEnum value = ReportTypeEnum.values()[ordinal];
请注意数组边界。
请注意,每次调用 values()
返回一个新克隆的数组,可能会以负面的方式影响性能。如果要经常调用,可能需要缓存数组。
Note that every call to values()
returns a newly cloned array which might impact performance in a negative way. You may want to cache the array if it's going to be called often.
。
这篇关于从枚举顺序转换为枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!