I have an enum in Java:
我有一个Java的枚举:
public enum Months
{
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
}
I want to access enum values by index, e.g.
我想通过索引访问枚举值,例如
Months(1) = JAN;
Months(2) = FEB;
...
How shall I do that?
我该怎么做?
5 个解决方案
#1
177
Try this
尝试这个
Months.values()[index]
#2
18
Here's three ways to do it.
这有三种方法。
public enum Months {
JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6), JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12);
int monthOrdinal = 0;
Months(int ord) {
this.monthOrdinal = ord;
}
public static Months byOrdinal2ndWay(int ord) {
return Months.values()[ord-1]; // less safe
}
public static Months byOrdinal(int ord) {
for (Months m : Months.values()) {
if (m.monthOrdinal == ord) {
return m;
}
}
return null;
}
public static Months[] MONTHS_INDEXED = new Months[] { null, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
}
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
public class MonthsTest {
@Test
public void test_indexed_access() {
assertEquals(Months.MONTHS_INDEXED[1], Months.JAN);
assertEquals(Months.MONTHS_INDEXED[2], Months.FEB);
assertEquals(Months.byOrdinal(1), Months.JAN);
assertEquals(Months.byOrdinal(2), Months.FEB);
assertEquals(Months.byOrdinal2ndWay(1), Months.JAN);
assertEquals(Months.byOrdinal2ndWay(2), Months.FEB);
}
}
#4
3
Check out the java tutorial on enum (example with planets)
查看enum上的java教程(有行星的例子)
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
#5
1
I just tried the same and came up with following solution:
我只是尝试了相同的并提出了以下解决方案:
public enum Countries {
TEXAS,
FLORIDA,
OKLAHOMA,
KENTUCKY;
private static Countries[] list = Countries.values();
public static Countries getCountry(int i) {
return list[i];
}
public static int listGetLastIndex() {
return list.length - 1;
}
}
The class has it's own values saved inside an array, and I use the array to get the enum at indexposition. As mentioned above arrays begin to count from 0, if you want your index to start from '1' simply change these two methods to:
该类将自己的值保存在数组中,并使用该数组来获取indexposition的枚举。如上所述,数组从0开始计数,如果您希望索引从'1'开始,只需将这两个方法更改为:
public static String getCountry(int i) {
return list[(i - 1)];
}
public static int listGetLastIndex() {
return list.length;
}
Inside my Main I get the needed countries-object with
在我的主要内部,我得到了所需的国家 - 对象
public static void main(String[] args) {
int i = Countries.listGetLastIndex();
Countries currCountry = Countries.getCountry(i);
}
which sets currCountry to the last country, in this case Countries.KENTUCKY.
将currCountry设置为最后一个国家,在本例中为Countries.KENTUCKY。
Just remember this code is very affected by ArrayOutOfBoundsExceptions if you're using hardcoded indicies to get your objects.
请记住,如果您使用硬编码的标记来获取对象,则此代码会受到ArrayOutOfBoundsExceptions的影响。
#1
177
Try this
尝试这个
Months.values()[index]
#2
18
Here's three ways to do it.
这有三种方法。
public enum Months {
JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6), JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12);
int monthOrdinal = 0;
Months(int ord) {
this.monthOrdinal = ord;
}
public static Months byOrdinal2ndWay(int ord) {
return Months.values()[ord-1]; // less safe
}
public static Months byOrdinal(int ord) {
for (Months m : Months.values()) {
if (m.monthOrdinal == ord) {
return m;
}
}
return null;
}
public static Months[] MONTHS_INDEXED = new Months[] { null, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
}
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
public class MonthsTest {
@Test
public void test_indexed_access() {
assertEquals(Months.MONTHS_INDEXED[1], Months.JAN);
assertEquals(Months.MONTHS_INDEXED[2], Months.FEB);
assertEquals(Months.byOrdinal(1), Months.JAN);
assertEquals(Months.byOrdinal(2), Months.FEB);
assertEquals(Months.byOrdinal2ndWay(1), Months.JAN);
assertEquals(Months.byOrdinal2ndWay(2), Months.FEB);
}
}
#3
#4
3
Check out the java tutorial on enum (example with planets)
查看enum上的java教程(有行星的例子)
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
#5
1
I just tried the same and came up with following solution:
我只是尝试了相同的并提出了以下解决方案:
public enum Countries {
TEXAS,
FLORIDA,
OKLAHOMA,
KENTUCKY;
private static Countries[] list = Countries.values();
public static Countries getCountry(int i) {
return list[i];
}
public static int listGetLastIndex() {
return list.length - 1;
}
}
The class has it's own values saved inside an array, and I use the array to get the enum at indexposition. As mentioned above arrays begin to count from 0, if you want your index to start from '1' simply change these two methods to:
该类将自己的值保存在数组中,并使用该数组来获取indexposition的枚举。如上所述,数组从0开始计数,如果您希望索引从'1'开始,只需将这两个方法更改为:
public static String getCountry(int i) {
return list[(i - 1)];
}
public static int listGetLastIndex() {
return list.length;
}
Inside my Main I get the needed countries-object with
在我的主要内部,我得到了所需的国家 - 对象
public static void main(String[] args) {
int i = Countries.listGetLastIndex();
Countries currCountry = Countries.getCountry(i);
}
which sets currCountry to the last country, in this case Countries.KENTUCKY.
将currCountry设置为最后一个国家,在本例中为Countries.KENTUCKY。
Just remember this code is very affected by ArrayOutOfBoundsExceptions if you're using hardcoded indicies to get your objects.
请记住,如果您使用硬编码的标记来获取对象,则此代码会受到ArrayOutOfBoundsExceptions的影响。