显示哈希表

时间:2015-04-23 00:34:05

标签: java hash

我无法弄清楚如何显示搜索方法的结果。我把字符串放在需要显示结果的地方。

这是客户端类

public class Client
{
    private String name;
    private String city;

    public Client(String name, String city)
    {
        this.name = name;
        this.city = city;
    }

    public String toString()
    {
        return name + " " + city;
    }

    public String getName()
    {
        return name;
    }

    public String getCity()
    {
        return city;
    }
}

这是我的Hashtable类

public class Hashtable {
    private int n;
    private Client[] table;

    public Hashtable(int n) {
        this.n = n;
        table = new Client[n];
    }

    public int hashFunction(String key) {
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        sum = sum%n;
        return sum;
    }

    public String search(String key) {       
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        sum = sum%n;
        if (key.equals(table[sum])) {
            return ("Returns the toString from the client class here");
        } else if (table[sum] == null) {
            return null;
        } else {
            while (table[sum] != null) {
                sum++;
            }
            return ("Returns the toString from the client class here");
        }
    }

    public boolean insert(Client myClient) {
        int counter = 0;
        String temp = myClient.getName();
        boolean ret = false;
        int tempSum = 0;
        for (int i = 0; i < temp.length(); i++) {
            tempSum += (int) temp.charAt(i);
        }
        tempSum = tempSum%n;
        if (table[tempSum] == null) {
            table[tempSum] = myClient;
            ret = true;
        } else {
            while (table[tempSum] != null) {
                if(tempSum == table.length){
                    tempSum = -1;
                }
                tempSum++;
                counter++;
            }
            if(counter != n){
                ret = true;  
                table[tempSum] = myClient;
            }
        } 
        return ret;
    }
}

1 个答案:

答案 0 :(得分:0)

在比较搜索方法中的键时,请确保与名称进行比较。

将表[sum]更改为表[sum] .getName()????我修改了代码并为更改添加了注释。

public class Hashtable {
    private int n;
    private Client[] table;

    public Hashtable(int n) {
        this.n = n;
        table = new Client[n];
    }

    public int hashFunction(String key) {
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        sum = sum%n;
        return sum;
    }

    public String search(String key) {       
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        sum = sum%n;
        if (key.equals(table[sum].getName())) {  //This Should be table[sum].getName()
            return ("Returns the toString from the client class here");
        } else if (table[sum] == null) {
            return null;
        } else {
            while (table[sum] != null) {
                sum++;
            }
            return ("Returns the toString from the client class here");
        }
    }

    public boolean insert(Client myClient) {
        int counter = 0;
        String temp = myClient.getName();
        boolean ret = false;
        int tempSum = 0;
        for (int i = 0; i < temp.length(); i++) {
            tempSum += (int) temp.charAt(i);
        }
        tempSum = tempSum%n;
        if (table[tempSum] == null) {
            table[tempSum] = myClient;
            ret = true;
        } else {
            while (table[tempSum] != null) {
                if(tempSum == table.length){
                    tempSum = -1;
                }
                tempSum++;
                counter++;
            }
            if(counter != n){
                ret = true;  
                table[tempSum] = myClient;
            }
        } 
        return ret;
    }
}

如果有帮助,请告诉我