简体   繁体   English

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

[英]Is it possible to use a primitive type (int) in as a generic type in Java?

Specifically, with a SortedMap<Vector<String>, int> I get "dimensions expected after this ( int ) token."具体来说,使用SortedMap<Vector<String>, int>我得到“此 ( int ) 标记之后的预期尺寸”。 Help!帮助!

No, this is not possible.不,这是不可能的。 Use Integer instead.请改用Integer Autoboxing takes care of the rest (ie for most purposes you can program as if you had actually used int because Java converts to and from Integer automatically for you).自动装箱负责其余的工作(即,在大多数情况下,您可以像实际使用int一样进行编程,因为 Java 会自动为您转换为 Integer 或从Integer转换)。

Konrad is correct.康拉德是正确的。 Alternately, you can use the trove class TObjectIntHashMap to map Objects to primitive ints.或者,您可以使用 trove 类 TObjectIntHashMap 将对象映射到原始整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM