java - HashSet<int> 为何有错误?
怪我咯
怪我咯 2017-04-18 10:50:54
0
4
806

代码里用了HashSet<int>
结果出错,说

 Syntax error, insert "Dimensions" to complete ReferenceType
 

改成HashSet<Integer>才OK
为何?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
洪涛

The type parameters in generics must be a subclass of Object, so int cannot be used, but the wrapper type Integer of int should be used.

大家讲道理

int is a basic data type, so it won’t work; you should use its corresponding packaging class, Interger

左手右手慢动作

Simply put, because int is a basic type and does not contain the methods required in the collection framework. Take this as an example. Even hashCode is not implemented. How to calculate the hash value? So you need to use Integer.

The types in generics must be subclasses of Object.

Ty80

int is the data type, and Integer is the packaging class. What should be placed in generics is the object type

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template