1

私の割り当てでは、文字列の HashTable を作成する必要があります。整数を使用して HashTable を作成しましたが、完全に機能しますが、現在問題が発生しています。

入力した文字列を取得java.lang.NumberFormatExceptionsし続けていますが、なぜこれが起こっているのかわかりません。HashTable クラスに使用しているコードは次のとおりです。

public class HashTable {
    private SortedList[] hashArray;
    private int arraySize;

    public HashTable(int size) {
        arraySize = size;
        hashArray = new SortedList[arraySize];

        for(int i = 0; i < arraySize; i++) {
            hashArray[i] = new SortedList();
        }
    } // END HashTable()

    public void displayTable() {
        for(int i = 0; i < arraySize; i++) {
            System.out.print(i + ". ");
            hashArray[i].displayList();
        }
    } // END displayTable()

    public int hash(String key) {
        int hashkey = Integer.parseInt(key);
        return hashkey % arraySize;
    } // END hash()

    public void insert(Link link) {
        String key = link.getKey();
        int hashVal = hash(key);
        hashArray[hashVal].insert(link);
    } // END insert()

    public void delete(String key) {
        int hashVal = hash(key);
        hashArray[hashVal].delete(key);
    } // END delete()
} // END HashTable

これはリストのコードです:

public class SortedList {
    private Link first;

    public void SortedList() {
        first = null;
    } //END SortedList()

    public void insert(Link link) {
        String key = link.getKey();
        Link previous = null;
        Link current = first;

        while(current != null && key.compareTo(current.getKey()) > 0) {
            previous = current;
            current = current.getNext();
        }

        if(previous == null) {
            first = link;
        } else {
            previous.setNext(link);
            link.setNext(current);
        }
    } // END insert()

    public void delete(String key) {
        Link previous = null;
        Link current = first;

        while(current != null && !key.equals(current.getKey())) {
            previous = current;
            current = current.getNext();
        }

        if(previous == null) {
            first = first.getNext();
        } else {
            previous.setNext(current.getNext());
        }
    } // END delete()

    public Link find(String key) {
        Link current = first;

         while(current != null && current.getKey().compareTo(key) <= 0) {
            if(current.getKey().equals(key)) {
                 return current;
            }
            current = current.getNext();
        }
        return null;
    } // END find()

    public void displayList() {
        System.out.print("List (first -> last): ");
        Link current = first;
        while(current != null) {
            current.displayLink();
            current = current.getNext();
        }
        System.out.println();
    } // END displayList()
} //END SortedList()

Link クラスは基本的なものにすぎません。次の行でエラーがスローされます。

        int hashkey = Integer.parseInt(key);

ハッシュ関数で、私の HashTable クラスで。

編集:私が求められていることを理解していることから、文字列を整数に変換し、ハッシュ関数を実行してその位置のインデックスを取得する必要があります。

4

4 に答える 4

1

String keyハッシュ キーを使用する目的で を整数に変換するには、 を使用しますkey.hashCode()

于 2013-07-15T15:51:49.737 に答える
1

ここで文字列を整数に変換しようとしているように見えますが、文字列は実際には数値を表していません

 int hashkey = Integer.parseInt(key);

String キーのハッシュコードを取得したい場合は、単純に呼び出すことができます。

int hashkey = key.hashCode();
于 2013-07-15T15:51:52.677 に答える
1

有効な整数文字列ではない文字列を渡しています。JavaDocに従って:_Integer#parseInt()

スロー:

NumberFormatException- 文字列に解析可能な整数が含まれていない場合。

文字列を整数に変換する別の方法を見つけてください。ヒント: String#hashCode().

于 2013-07-15T15:51:54.667 に答える