Map

1.Map 有泛型的必要性---防止运行时报 incompatible types: Object cannot be converted to int 

Map 中未使用泛型,Map 进行 get 操作赋值给 int[ ] 的时候报如下错误

报错:incompatible types: Object cannot be converted to int

原因:没有使用泛型约束,Map 的默认类型为 Object,要赋值的类型为 int,类型不匹配,故报错

解决办法:加上泛型

 Map<Integer,Integer> hashMap = new HashMap<>();

 

posted @ 2020-12-13 12:50  快走中的擦边球  阅读(189)  评论(0编辑  收藏  举报