java吧 关注:1,307,939贴子:12,881,694
  • 8回复贴,共1

大神求助啊!!!!

只看楼主收藏回复

int i = (Integer)m1.get("two");为什么要强制转换?m1.get("two");得到的本来就是Integer对象啊!怎么还要转换?
import java.util.*;
public class TestMap {
public static void main(String args[]) {
Map m1 = new HashMap();
m1.put("one", 1);//自动打包,注意与上面代码的区别
m1.put("two", 2);
m1.put("three", 3);
System.out.println(m1)
if(m1.containsKey("two")) {
int i = (Integer)m1.get("two");//自动解包
System.out.println(i);
}
}
}


1楼2016-10-06 01:25回复
    你没定义泛型 得到的是object型


    IP属地:四川来自iPhone客户端2楼2016-10-06 01:54
    收起回复
      2026-08-23 09:10:05
      广告
      不感兴趣
      开通SVIP免广告
      一楼正解!jdk1.5的泛型就是解决你问题的所在


      IP属地:四川来自iPhone客户端4楼2016-10-06 01:59
      收起回复
        放进去的two程序Java识别的是字符串类型的


        来自Android客户端5楼2016-10-07 00:26
        回复