Why can't I initialize a Map<int, String>? [duplicate]

天大地大妈咪最大 提交于 2020-01-02 02:21:10

问题


I want to store a set of int/String values, but the ints are not necessarily incremental, meaning the data can be:

<1, "first">, <3, "second">, <9, "third">. 

So I'm trying to create the c# equivalent of a Dictionary<int, string> but it just fails to compile, saying "Syntax error on token "int", Dimensions expected after this token" on the line:

private Map<int, String> courses;

Can anyone please tell me why this is? And a good alternative to creating an object as a placeholder for the int and String, then using an array to store them?


回答1:


You cannot use primitive types as generic type arguments.

Use

private Map<Integer, String> courses;

See more restrictions on Generics here.

Dev's contribution: the JLS specifies that only reference types (and wildcards) can be used as generic type arguments.



来源:https://stackoverflow.com/questions/18729550/why-cant-i-initialize-a-mapint-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!