2015-04-03 83 views
0

我正在開發一個項目,我必須編寫一個類SeparateChainingMap,它代表一個單獨的鏈式哈希表並實現Map接口。每當我運行我的代碼時,我都會收到由我的插入方法引起的空指針異常。任何人都可以提供指導我做錯了什麼?誰能告訴我爲什麼下面的代碼生成空指針異常?

我已經在下面列出了我所有的代碼。我調用insert方法時拋出NullPointerException。

import java.util.LinkedList; 
import java.util.*; 


public class SeparateChainingMap<K extends Comparable<? super K>, V> implements Map<K,V> { 

public static final int INITIAL_SIZE = 10; 
private int currentSize; 
private LinkedList<Pair<K,V>>[] table; 


@SuppressWarnings("unchecked") 
public SeparateChainingMap() { 
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE]; 
} 

@SuppressWarnings("unchecked") 
public SeparateChainingMap (int size) { 
    LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE]; 
    for (int i=0; i<table.length; i++) { 
     table[i] = new LinkedList<Pair<K,V>>(); 
    } 
} 

//Introducing the hash function 
private int myHash(Pair<K,V> x) { 
    int hashValue = x.hashCode(); 
    //divide the hashValue by mod table length 
    hashValue %= table.length; 

    if(hashValue<0) { 
     hashValue += table.length; 
    } 
    System.out.println(hashValue); 
    return hashValue; 
} 

//rehash function 
@SuppressWarnings("unchecked") 
private void rehash() { 
    LinkedList<Pair<K,V>>[] oldTable = table; 

    //Create new double-sized empty tables 
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[2 * table.length]; 

    for (int j=0; j< table.length; j++) { 
     table[j] = new LinkedList<>(); 
    } 

    //Copy table over 
    currentSize = 0; 
    for (int i=0; i<oldTable.length; i++) { 
     for(int j=0; j<oldTable[i].size(); j++){ 
      insert(oldTable[i].get(j)); 

     } 

    }  

} 

//Find an item in the hash table. returns true if x is found 
public boolean contains(Pair<K,V> x) { 
    LinkedList<Pair<K,V>> thisList = table[myHash(x)]; 
    return thisList.contains(x); 
} 

//Method for inserting a pair into the hash table 
public void insert(Pair<K,V> x) { 
    LinkedList<Pair<K,V>> thisList = table[myHash(x)]; 

    if(!thisList.contains(x)) 
    { 
     thisList.add(x); 
    } 

    if (++currentSize > table.length) { 
     rehash(); 
    } 


} 

public void remove (Pair<K,V> x) { 
    LinkedList<Pair<K,V>> thisList = table[myHash(x)]; 

    if(thisList.contains(x)) { 
     thisList.remove(x); 
     currentSize--; 
    } 
} 

public void makeEmpty() 
{ 
    for(int i = 0; i<table.length; i++) { 
     table[i].clear(); 
    currentSize = 0; 
    } 
} 

//map functions 
@SuppressWarnings("unchecked") 
public V get (K key) { 
    Pair<K,V> pair = new Pair<K,V>(key,null); 
    int i = myHash(pair); 

    for(int j=0; j<table.length; j++) { 

     if(i == j) { 
      for(int k=0; k<table[j].size(); k++) 
      { 
       if(key.equals(table[j].get(k).key)){ 
        V value = table[j].get(k).value; 
        return value; 
       } 
      } 


     } 

    } 


    return null; 
} 

public void put(K key, V value) { 
    Pair<K,V> pair = new Pair<K,V>(key, value); 
    insert(pair); 

} 


public static void main(String args[]) { 
@SuppressWarnings("unchecked") 

SeparateChainingMap<String,Integer> SCM = new SeparateChainingMap<>(); 

SCM.put("Alice", 243); 

}

+2

附加一個調試器。打破例外。玩。問題總是「一樣」;找出表達式爲空並且向後工作。 – user2864740 2015-04-03 01:50:11

回答

1
@SuppressWarnings("unchecked") 
public SeparateChainingMap() { 
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE]; 
} 

此構造的表[I]不啓動。它應該如下:

@SuppressWarnings("unchecked") 
public SeparateChainingMap (int size) { 
    LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE]; 
    for (int i=0; i<table.length; i++) { 
     table[i] = new LinkedList<Pair<K,V>>(); 
    } 
} 
相關問題