使用HashTableMap计算第n个Fibonacci数

时间:2018-04-18 09:47:15

标签: java

我正在尝试实现fibCalc2方法来计算第n个Fibonacci数,但是我得到了空指针异常,我不知道如何进一步。我的解决方案有意义吗?任何帮助将不胜感激。

注意:HashTableMap也是由我自己实现的,因此它与Java的实现不同。

更新:代码最后一行的NullPointerException

以下是代码:

private static int callCount2;
private static Map<Integer, Long> ansMap = new LLQHashTableMap(10);



public static long fibCalc2(int n) {
    if(n == 0 ||n == 1) return n;
    if(ansMap.getSize() <= 2){
        ansMap.define(0, (long) 0);
        ansMap.define(1, (long) 1);
    }
    long tempVal1, tempVal2;
    try {
        long temp = ansMap.remove(n);
        ansMap.define(n, temp);
    } catch (Exception ex){

        try {
            long temp = ansMap.remove(n-1);
            ansMap.define(n, temp);
            tempVal1 = ansMap.getValue(n-1);
        } catch (Exception ex1){
            tempVal1 = fibCalc2(n-1);
        }
        try {
            long temp = ansMap.remove(n-2);
            ansMap.define(n, temp);
            tempVal2 = ansMap.getValue(n-2);
        } catch (Exception ex1){
            tempVal2 = fibCalc2(n-2);
        }
        ansMap.define(n, tempVal1+tempVal2);
    }
    callCount2++;
    return ansMap.getValue(n);
}

我的地图界面:

package adt;

import impl.KeyValuePair;

/**
 * A generic map
 * @param <K>
 * @param <V>
 */
public interface Map<K, V> {

    /**
     * Adds a new key-value mapping to the map, which will
     * replace a previous key-value mapping that has the
     * same key, if it exists
     *
     * @param key of the key-value pair to be added
     * @param value of the key-value pair to be added
     */
    public void define(K key, V value);

    /**
     * Returns the value associated with the given key
     * in the map, if one exists, or null if the key is 
     * not in the map
     *
     * @param key whose value we want to return
     * @return value associated with the given key
     */
    public V getValue(K key);

    /**
     * Removes the key-value pair from the map that has
     * the given key value, and returns the associated value 
     * if it exists; if the key is not in the map, the map
     * remains unchanged, and null is returned
     *
     * @param key of the key-value pair that we want to remove
     * @return value associated with the given key before removal
     */
    public V remove(K key);

    /**
     * Removes and returns some key-value pair from the map, 
     * if the map is not empty; if the map is empty, an 
     * exception is thrown
     *
     * @return some key-value pair from the map
     * @throws Exception if the map is empty
     */
    public KeyValuePair<K, V> removeAny() throws Exception;

    /**
     * @return the number of key-value pairs in the map
     */
    public int getSize();

    /**
     * Removes all key-value pairs from the map
     */
    public void clear();

    /**
     * @return a String representation of the map
     */
    @Override
    public String toString();
}

1 个答案:

答案 0 :(得分:1)

NPE发生在

long temp = ansMap.remove(n);

当n = 2时,因为remove(n)将返回null,并且您不能将null赋给基本类型变量。