簡體   English   中英

Java HashTable實現get方法返回null?

[英]Java HashTable Implementation get method returning null?

因此,我需要編寫一個程序來接收包含NFL球隊名稱和得分的17個文件(例如一個文件包含所有32個球隊的得分,而另一個文件可能包含30個相同球隊的30個不同得分,但是當然要省略兩個球隊)。 我的教授為我們提供了一個HashTable實現供使用,它通過在HashTable中的每個占用索引處創建某種LinkedList來處理沖突(我經驗不足,所以很抱歉,如果我不了解全部信息,術語正確,但希望您理解我的意思)。 我已經成功導入了所有文件和數據,並將它們輸入到HashTable中,並使用了教授給我們的沖突處理方法。 但是,每當我嘗試為任何鍵調用get方法時,它都會返回“ null”。 為什么是這樣? 我之所以這樣問,是因為我需要找到每個團隊的平均團隊得分,但由於get方法返回的是null,所以我不知道這樣做。 任何幫助將非常感激!

碼:

HashEntry:

public class HashEntry 
{
private String key;
private Double value;
private HashEntry next;

public HashEntry(String key, Double value) 
{
    this.key = key;
    this.value = value;
}

public String getKey() 
{
    return key;
}

public void setKey(String key) 
{
    this.key = key;
}

public Double getValue() 
{
    return value;
}

public void setValue(Double value) 
{
    this.value = value;
}

public HashEntry getNext() 
{
    return next;
}

public void setNext(HashEntry next) 
{
    this.next = next;
}

public boolean isNextEmpty()
{
    if(next.equals(null))
        return true;
    return false;
}

哈希表:

public class HashTable implements StringHashTable 
{
private HashEntry[] dataArray;
private int size;

public HashTable() 
{
    dataArray = new HashEntry[1000];
    size = 0;
}

private int hash(String key) 
{
    int sum = 0;
    for(int i = 0; i < key.length(); i++)
        sum += (int)key.charAt(i);

    return sum % dataArray.length;
}

@Override
public void put(String key, Double value) 
{
    HashEntry entry = new HashEntry(key, value);
    int indexToPut = hash(key);
    HashEntry cursor = dataArray[indexToPut];
    if(cursor != null) 
    {
        while(cursor.getNext() != null && cursor.getKey() != key) 
        {
            cursor = cursor.getNext();
        }
        if(cursor.getKey() != key) 
        {
            cursor.setNext(entry);
        } 
        else 
        {
            cursor.setValue(value);
        }
    } 
    else 
    {
        dataArray[indexToPut] = entry;
    }
    size++;
}

@Override
public Double get(String key) 
{
    int indexToGet = hash(key);
    HashEntry cursor = dataArray[indexToGet];
    while(cursor != null && cursor.getKey() != key) 
    {
        cursor = cursor.getNext();
    }
    if (cursor == null) 
    {
        return null;
    }
    return cursor.getValue();
}

@Override
public int size() 
{
    return size;
}

@Override
public void remove(String key) 
{
    int indexToRemove = hash(key);
    HashEntry cursor = dataArray[indexToRemove];
    HashEntry prev = null;
    while(cursor != null && cursor.getKey() != key) 
    {
        prev = cursor;
        cursor = cursor.getNext();
    }
    if (cursor != null) 
    {
        if (prev == null) 
        {
            dataArray[indexToRemove] = cursor.getNext();
        } 
        else 
        {
            prev.setNext(cursor.getNext());
        }
        size--;
    }
}

public String toString() 
{
    String res = "";
    for(HashEntry entry : dataArray) 
    {
        if (entry != null) 
        {
            HashEntry cursor = entry;
            while(cursor != null) 
            {
                res += cursor.getKey() + " = " + cursor.getValue() + "\n";
                cursor = cursor.getNext();
            }
        }
    }
    return res;
}

驅動類別

public class Project3 
{
static HashTable table = new HashTable();   
static HashMap<String, Double> table1 = new HashMap<String, Double>();
public static void main(String[] args) throws IOException
{
    //HashTableImpl<String, Double> table = new HashTableImpl<String, Double>();

    if (args.length < 1) 
    {
        System.out.println("Error: Directory name is missing");
        System.out.println("Usage: java scoreProcess directory_name");
        return;
    }

    File directory = new File(args[0]); // args[0] contains the directory name
    File[] files = directory.listFiles(); // get the list of files from that directory

    File file;
    Scanner input;

    // process the arguments stores in args
    for (int i = 0; i < files.length; i++) 
    {
        input = new Scanner(files[i]);

        //System.out.println("\nCurrent file name: " + files[i].getName());

        // no error checking done here, add your own
        String name;
        Double score;
        while(input.hasNext())
        {
            name = "";
            while(!input.hasNextDouble())
            {
                name += input.next() + " ";
            }
            score = input.nextDouble();
            //System.out.println("Name: " + name + " Score: " + score);
            table.put(name, score);
            table1.put(name, score);
        }
    }
    System.out.println("\n");
    System.out.println(table.toString());
    System.out.println(table.size());
    //System.out.println(table1.toString());
    System.out.println(table.get("Minnesota"));
}
}

驅動程序輸出: https : //drive.google.com/file/d/0BwujWiqVRKKsNW52N1M2UllCeHc/view?usp=sharing

示例文本文件:

New England 27
Indianapolis 24
Tennessee 17
Miami 7
St. Louis 17
Arizona 10
Seattle 21
New Orleans 7
NY Jets 31
Cincinnati 24
Pittsburgh 24
Oakland 21
Washington 16
Tampa Bay 10
San Diego 27
Houston 20
Jacksonville 13
Buffalo 10
Detroit 20
Chicago 16
Cleveland 20
Baltimore 3
Atlanta 21
San Francisco 19
Philadelphia 31
NY Giants 17
Minnesota 35
Dallas 17
Denver 34
Kansas City 24
Green Bay 24
Carolina 14

您的代碼中有許多錯誤。 初讀時最明顯的是:

  • 在map的實現中將字符串與equals not ==比較
  • 在將名稱放入地圖之前,請勿在名稱末尾添加空格

我對查看代碼的主要建議是學習與編寫代碼一起開發單元測試。 在這種情況下,您應該進行測試,表明HashEntryHashTable中使用它之前可以達到預期的效果,在繼續從文件中讀取值並將其放入映射之前,還應該進行徹底的測試。 或者,如果使用模擬,則可以相反的順序進行。 但是,如果最后進行測試,則很難知道出了什么問題。 學習構建單元測試(最好在編寫代碼之前),這些類型的問題將更容易發現和解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM