Java 显示哈希表中的搜索结果

Java 显示哈希表中的搜索结果,java,hash,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()

我不知道如何显示搜索方法的结果。我把字符串放在需要显示结果的地方

这是客户机类

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;
    }
}
这是我的哈希表类

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

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

将表[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;
    }
}

让我知道这是否有助于添加实际使用类但无法执行指定操作的非常短的代码。完整、最小、可验证。是否查找返回表[sum]。toString?