java enum ArrayList - is this possible? [duplicate]
Possible Duplicate:
Adding enum type to a list
I have an enum class:
publ开发者_运维问答ic enum MyEnum {
ONE, TWO, THREE;
}
And in another class, MyClass.java, I want to have the following
ArrayList<MyEnum> list;
is this possible? because I'm encountering some issues.
There should be no problem doing that.
This code compiles fine:
package example;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main (String[] args) {
List<MyEnum> enums = new ArrayList<MyEnum>();
}
enum MyEnum { ONE, TWO, THREE;}
// no need for that ^ but added to match your question
}
Despite "some issues" you have, aren't you actually looking for an EnumSet?
EnumSet<MyEnum> set = EnumSet.of(MyEnum.ONE, MyEnum.THREE);
精彩评论