当前位置: 代码迷 >> J2SE >> Map 添加 简单类型与Object类型有关问题
  详细解决方案

Map 添加 简单类型与Object类型有关问题

热度:495   发布时间:2016-04-24 14:01:18.0
Map 添加 简单类型与Object类型问题
int   count   =   10;
Map   map   =   (Map)list.get(0);
map.put( "个数 ",count);

有的时候上面这个map.put是正确的,但是有的时候就会提示:
The   method   put(Object,   Object)   in   the   type   Map   is   not   applicable   for   the  
  arguments   (String,   int)

不允许用简单类型,
必须用map.put(new   String( "个数 "),new   Integer(count));才行。

请问这是为啥?

------解决方案--------------------
JDK 1.5 以上的版本有自动装箱功能,这段代码应该是没有问题的。

JDK 1.5 以下的版本没有这个功能,需要将基本类型转为包装类型,字符串用不着转的。
------解决方案--------------------
我从1.5以后开始用的 没碰到过 哈哈
------解决方案--------------------
The method put(Object, Object) in the type Map is not applicable for the arguments (String, int)

这个说的比较清楚了, int是基本数据类型,并不是一个对象,但是 put(Object, Object)这里要求的是2个对象。

map.put(new String( "个数 "),new Integer(count)),这里你看第2个参数就是一个对象了。


------解决方案--------------------
建议用泛型吧~~
  相关解决方案