Using int as a type parameter for java.util.Dictionary

后端 未结 6 2135
孤独总比滥情好
孤独总比滥情好 2021-02-06 22:51

When I try to declare a Dictionary as such:

private Dictionary map;

The compiler gives me the following error:

6条回答
  •  孤独总比滥情好
    2021-02-06 23:29

    Java collections only allow references not primitives. You need to use the wrapper classes (in this case java.lang.Integer) to do what you are after:

    private Dictionary map;
    

    they you can do things like:

    int foo = map.get("hello");
    

    and

    map.put("world", 42);
    

    and Java uses autoboxing/unboxing to deal with the details of the conversion for you.

    Here is a little description on it.

提交回复
热议问题