是否可以在Java中使用基本类型(int)作为泛型类型?

时间:2022-02-02 16:27:13

Specifically, with a SortedMap<Vector<String>, int> I get "dimensions expected after this (int) token." Help!

具体来说,使用SortedMap ,int>我得到“在此(int)标记之后预期的维度”。救命!

2 个解决方案

#1


No, this is not possible. Use Integer instead. Autoboxing takes care of the rest (i.e. for most purposes you can program as if you had actually used int because Java converts to and from Integer automatically for you).

不,这是不可能的。请改用Integer。 Autoboxing负责其余部分(即,对于大多数目的,您可以编程,就像您实际使用int一样,因为Java会自动为您转换为Integer和从Integer转换)。

#2


Konrad is correct. Alternately, you can use the trove class TObjectIntHashMap to map Objects to primitive ints.

康拉德是对的。或者,您可以使用trove类TObjectIntHashMap将对象映射到原始int。

#1


No, this is not possible. Use Integer instead. Autoboxing takes care of the rest (i.e. for most purposes you can program as if you had actually used int because Java converts to and from Integer automatically for you).

不,这是不可能的。请改用Integer。 Autoboxing负责其余部分(即,对于大多数目的,您可以编程,就像您实际使用int一样,因为Java会自动为您转换为Integer和从Integer转换)。

#2


Konrad is correct. Alternately, you can use the trove class TObjectIntHashMap to map Objects to primitive ints.

康拉德是对的。或者,您可以使用trove类TObjectIntHashMap将对象映射到原始int。