Java HashTable 实现get 方法返回null?

Java HashTable Implementation get method returning null?

所以我需要编写这个程序来接收 17 个包含 NFL 球队名称和分数的文件(比如一个文件包含所有 32 个球队的分数,而另一个文件可能包含 30 个相同球队的 30 个不同分数,但是当然省略了两支球队)。我的教授为我们提供了一个 HashTable 实现来使用,它通过在 HashTable 中每个占用的索引处创建某种 LinkedList 来处理冲突(我相当缺乏经验,所以如果我没有得到所有的,我很抱歉术语正确,但希望你明白我的意思)。我已成功导入所有文件和数据,并使用我的教授给我们的碰撞处理将它们输入到 HashTable 中。但是,每当我尝试为任何键调用 get 方法时,它 returns "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;
}

Driver Class:

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"));
}
}

Driver 输出: 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 而不是 ==
  • 在将名称放入地图之前,不要在名称末尾添加 space

我对您查看代码的主要建议是在编写代码的同时学习开发单元测试。在这种情况下,您应该进行测试以表明 HashEntryHashTable 中使用它之前达到了您的期望,在您继续从文件中读取值并将它们放入地图之前也应该进行彻底的测试。或者,如果您使用模拟,则可以按相反的顺序进行。但是尝试在最后进行测试会让我们更难知道哪里出了问题。学习构建单元测试(最好在编写代码之前),这些类型的问题将更容易找到和解决。