簡體   English   中英

顯示哈希表中的搜索結果

[英]Displaying search results from a hash table

我不知道如何顯示搜索方法的結果。 我將字符串放在需要顯示結果的地方。

這是客戶班

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

讓我知道是否有幫助

暫無
暫無

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

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