我有一个字符串的枚举,如在这个example。如何迭代Java枚举?
当我的当前元素是STRING_ONE时,如何切换到下一个字符串项目的枚举与例如++运算符?
喜欢的东西:
Strings myEnum = String.STRING_ONE;
myEnum++ ; // to have STRING_TWO for instance
?
编辑:我不想遍历所有这些枚举元素。这些STRING_ONE,STRING_TWO等都是某种状态。我想实现像
private void getNextstate(){
myCurrentstate++;
}
一个getNextState()
方法现在,我有这个,这似乎高效
private void getNextstate(){
if(myCurrentstate == Strings.STRING_ONE) myCurrentstate == Strings.STRING_TWO;
else if(myCurrentstate == Strings.STRING_TWO) myCurrentstate == Strings.STRING_THREE;
//..
else if(myCurrentstate == Strings.STRING_NINTYNINE) myCurrentstate == Strings.STRING_HUNDRET;
}
'X = Strings.values()[x.ordinal()+ 1]' – khelwood