質問

Is this possible? I don't think it is but I just want to make sure. This is what I was trying.

private Map enumToMap(Enum<?> e){
    EnumMap map = new EnumMap<e, String>(e.getClass()); // error can't resolve constructor for EnumMap
    for (History h : History.values()) {
        map.put(h, h.answer());
    }
    return map;
}
役に立ちましたか?

解決

I suspect that History is an enum. Otherwise you will have a bound-type error.

If the History type is an enum, then the root of your problem is in the public EnumMap (Class< K > keyType) constructor, because as per javadoc, it:

Creates an empty enum map with the specified key type.

In your case, you're defining that the key type will be History and in the meantime you're passing an Enum.class to the constructor.

A valid definition would be:

new EnumMap<History, String>(History.class);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top