开发者

java enum ArrayList - is this possible? [duplicate]

开发者 https://www.devze.com 2023-03-26 18:37 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Adding enum type to a list
This question already has answers here: Closed 11 years ago.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消