为了账号安全,请及时绑定邮箱和手机立即绑定

Java泛型问题

Java泛型问题

潇湘沐 2019-03-06 14:15:10
第三行代码有警告,请问怎样去除。报错信息我注释在了警告行上面。class MyHashMap<K, V> {    public static final int SIZE = 1000;    //Type safety: The expression of type MyHashMap.Entry[] needs unchecked conversion to conform to MyHashMap.Entry<K,V>[]    public Entry<K, V>[] tables = new Entry[SIZE];        public void put(K key, V value) {        int index = key.hashCode() % SIZE;        while(tables[index] != null) {            index++;        }        tables[index] = new Entry<K, V>();        tables[index].key = key;        tables[index].value = value;        tables[index].hash = key.hashCode();    }        public V get(K key) {        int index = key.hashCode() % SIZE;        while(tables[index] != null && key.hashCode() !=  tables[index].hash                &&!key.equals(tables[index].key)) {            index++;        }        return tables[index].value;    }        static class Entry<K, V> {        int hash;        K key;        V value;    }}
查看完整描述

2 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

@suppresswarnings("unchecked")


查看完整回答
反对 回复 2019-04-24
?
aluckdog

TA贡献1847条经验 获得超7个赞

泛型类型是不能定义数组的。。


查看完整回答
反对 回复 2019-04-24
  • 2 回答
  • 0 关注
  • 401 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号