0

给出“不能从静态上下文引用的非静态变量”

这是模板类的以下代码

public class BiHashMap<K1, K2, V>{

    private final Map<K1, Map<K2, V>> mMap;

    public BiHashMap() {
        mMap = new HashMap<K1, Map<K2, V>>();
    }

    public V put(K1 key1, K2 key2, V value) {
        Map<K2, V> map;
        if (mMap.containsKey(key1)) {
            map = mMap.get(key1);
        } else {
            map = new HashMap<K2, V>();
            mMap.put(key1, map);
        }

        return map.put(key2, value);
    }

}

public static void main(String[] args) {
    BiHashMap<double,double,double> table1 = new BiHashMap<double,double,double>();
    table1.put(0.375,1,350);

我尝试为 double 创建一个新课程,但错误仍然存​​在

public class dbble{
    double number;

    dbble(double x){
        number=x;
    }
}
4

1 回答 1

0

我已经在上面测试了您的代码并对其进行了编辑以使其运行:(您必须将main函数放入 中class,您需要使用wrapper typefor double,并在调用函数时将您放入的值转换为)。

public class BiHashMap<K1, K2, V> {

private final Map<K1, Map<K2, V>> mMap;

public BiHashMap() {
    mMap = new HashMap<K1, Map<K2, V>>();
}

public V put(K1 key1, K2 key2, V value) {
    Map<K2, V> map;
    if (mMap.containsKey(key1)) {
        map = mMap.get(key1);
    } else {
        map = new HashMap<K2, V>();
        mMap.put(key1, map);
    }

    return map.put(key2, value);
}

public static void main(String[] args) {
    BiHashMap<Double, Double, Double> table1 = new BiHashMap<Double, Double, Double>();
    table1.put(0.375, Double.valueOf(1), Double.valueOf(350));
}
}
于 2017-04-30T23:18:18.700 回答