Java泛型问题

第三行代码有警告,请问怎样去除。报错信息我注释在了警告行上面。


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;

    }

}


潇湘沐
浏览 402回答 2
2回答

慕尼黑的夜晚无繁华

@suppresswarnings("unchecked")

aluckdog

泛型类型是不能定义数组的。。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java