-1

クラスの配列を書く正しい方法が見つからないようです。この形式では、コンパイル時にエラーはスローされませんが、配列/クラスを使用しようとするとエラーが発生します。クラスの配列はHashTableという名前のクラスにあり(割り当てのために独自に作成する必要があります)、以下のコードでテストしています。

theHashTable.insert("aa", "ab");

HashTableクラスは次のとおりです。

編集:Aniketが指摘したように、fileNamesは初期化されていませんでした。以下でこれを修正しましたが、同じエラーが発生します。

private class HashTable {       
    private class Value {
        ArrayList<String> fileNames;
        String word;
        Value() {
            fileNames = new ArrayList<String>();
        }
    }

    private int currentSize = 101;
    private Value[] items;
    private HashTable() {
        items = new Value[currentSize];
        for (int i = 0; i < currentSize; i++) items[i] = new Value();
    }

    private int hash(String in) {
        int out = 0;
        for (int i = 0; i < in.length(); i++) out += 37*out+in.charAt(i);
        out %= currentSize;
        if (out < 0) out += currentSize;
        return out;
    }

    public void insert(String inW, String inF) {
        int index = hash(inW);
        index = 0;
        if (items[index].word.length() == 0) {
            items[index].word = inW;
            items[index].fileNames.add(inF);
        }
        else if (items[index].word.compareTo(inW) == 0) items[index].fileNames.add(inF);
        else System.out.println("Collision");
    }
}
4

2 に答える 2

2

fileNamesValuesクラスのArrayListが初期化されることはありません。ValuesおよびInitializeのコンストラクターを記述しfileNamesます。

于 2012-12-05T03:06:51.287 に答える
1

初期化(サンプルコードの3行目)を使用して内部クラス値を次のように宣言fileNamesします。

     ArrayList<String> fileNames = new  ArrayList<String>();
于 2012-12-05T03:16:49.823 に答える