简体   繁体   English

如何在我的哈希表中修复这个 ArrayIndexOutOfBoundsException?

[英]How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

I was wondering if anyone could help me this ArrayIndexOutOfBoundsException that occurs when trying to run a tester class.我想知道是否有人可以帮助我尝试运行测试类时发生的这个 ArrayIndexOutOfBoundsException。

The exception occurs at the remove method within the hashtable file.异常发生在哈希表文件中的 remove 方法中。 I have tried switching out my code with my friend's code but that didn't work either.我试过用我朋友的代码切换我的代码,但这也不起作用。

Any help would be greatly appreciated任何帮助将不胜感激

=========================================================================== ================================================== ==========================

Here is the HashTable :这是哈希表:

public class HashTable {

Object[] hTable;
int mSize;
int size;

HashTable() {
    mSize = 101;
    hTable = new Object[mSize];
}

HashTable(int initCap) {
    mSize = initCap;
}

Object put(Object key, Object value) {
    if (size == mSize) {
        throw new IllegalStateException("No room within the hashtable");
    }

    int hashC = key.hashCode();
    int index = hashC % mSize;

    size++;

    while (index < hTable.length) {
        if (hTable[index] == null) {
            hTable[index] = new Entry(key, value);
            size++;
            return null;

        } else if (((Entry) hTable[index]).key.equals(key)) {
            Object prevVal = ((Entry) hTable[index]).val;
            hTable[index] = new Entry(key, value);
            return prevVal;

        } else if (((Entry) hTable[index]).rCheck) {
            hTable[index] = new Entry(key, value);

            while (index < hTable.length) {
                index++;
                if (hTable[index] == null) {
                    size++;
                    return null;
                } else if (((Entry) hTable[index]).key.equals(key)) {

                    Object prevVal = ((Entry) hTable[index]).val;
                    ((Entry) hTable[index]).remove();

                    return prevVal;
                }
            }
        }
        index++;
    }

    if (hTable[index] == null) {
        hTable[index] = new Entry(key, value);
        return null;

    } else {

        Object oldEntry = ((Entry) hTable[index]).val;
        hTable[index] = new Entry(key, value);
        return oldEntry;
    }

}

Object get(Object key) {
    int hashC = key.hashCode();
    int index = hashC % mSize;

    return ((Entry) hTable[index]).val;
}

Object remove(Object key) {
    int hashC = key.hashCode();
    int index = hashC % mSize;

    Object returnObj = null;

    while (hTable[index] != null) { //here is where the OutOfBounds error occurs
        if (((Entry) hTable[index]).key.equals(key)) {
            returnObj = ((Entry) hTable[index]).val;
            ((Entry) hTable[index]).remove();

            size--;
            break;
        }
        index++;
    }
    return returnObj;
}

int size() {
    return size;
}

@Override
public String toString() {
    String returnString = "";

    for (int i = 0; i < hTable.length; i++) {
        if (hTable[i] == null || ((Entry) hTable[i]).rCheck) {
            returnString += "dummy\n";
            continue;
        }

        returnString += "Index: " + i +
                " \n Key: " + ((Integer) (((Entry) hTable[i]).key)).intValue() % 101 +
                "\nValue: " + (String) (((Entry) hTable[i]).val) +
                "\n++++++++++++++++++++++++++++++++++++++++++++++\n";
    }

    return returnString;
}

private class Entry {
    Object key;
    public boolean rCheck;
    public Object val;

    Entry() {
        key = null;
        val = null;
        rCheck = false;
    }

    Entry(Object k, Object v) {
        key = k;
        val = v;
        rCheck = false;
    }

    Object value() {
        return val;
    }

    Object key() {
        return key;
    }

    void remove() {
        rCheck = true;
    }

    public String toString() {
        return "";
    }
}
}

Here is the hash table tester:这是哈希表测试器:

    import java.io.*;
    import java.util.*;

public class hashTest {
    public static void main(String args[]) throws FileNotFoundException {
        HashTable hashTable = new HashTable();
        Scanner fileRead = new Scanner(new File("data1.txt"));
        while(fileRead.hasNext()) {
            Object key = fileRead.next();
            fileRead.next();
            Object value = fileRead.nextLine();
            hashTable.put(key, value);
            System.out.println(hashTable.get(key));
        }
        Scanner fileRead2 = new Scanner(new File("data2.txt"));
        while(fileRead2.hasNext()){
            Object key = fileRead2.next();
            hashTable.remove(key);
            fileRead2.nextLine();

        }
        Scanner fileRead3 = new Scanner(new File("data3.txt"));
        while(fileRead3.hasNext()){
            Object key = fileRead3.next();
            fileRead3.next();
            Object value = fileRead3.nextLine();
            hashTable.put(key, value);

        }
        Scanner fileRead4 = new Scanner(new File("data4.txt"));
        while(fileRead4.hasNext()){
            Object key = fileRead4.next();
            fileRead4.next();
            Object value = fileRead4.nextLine();
            hashTable.put(key, value);

        }
    }
}

=========================================================================== ================================================== ==========================

In the shared google drive link below you will find a zip containing data inputs.在下面的共享谷歌驱动器链接中,您会找到一个包含数据输入的 zip。

https://drive.google.com/file/d/1iYrzWl9mtv_io3q7K1_m2EtPFUXGbC3p/view?usp=sharing https://drive.google.com/file/d/1iYrzWl9mtv_io3q7K1_m2EtPFUXGbC3p/view?usp=sharing

Your Problem is at this code block:你的问题是在这个代码块:

 while (index < hTable.length) {
    index++;
    if (hTable[index] == null)...

At the last iteration index will be the same as hTable.length .在最后一次迭代index将与hTable.length相同。 In your Example index will be 100 where the condition will be accepted.在您的示例中, index将是 100,其中条件将被接受。 In the next step index will be incremented: index = 101. At hTable[101] the ArrayIndexOutOfBoundsException will occur.在下一步中,索引将增加: index = 101。在hTable[101]处将发生ArrayIndexOutOfBoundsException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复此ArrayIndexOutOfBoundsException? - How do I fix this ArrayIndexOutOfBoundsException? 如何修复arrayindexoutofboundsexception错误? - How do I fix an arrayindexoutofboundsexception error? 如何解决此ArrayIndexOutOfBoundsException - How can I fix this ArrayIndexOutOfBoundsException 如何解决此运行时错误? 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - How do i fix this runtime error? Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 如何在此命令上修复此错误谢谢...线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 - How do I fix this error on this command thanks… Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 如何修复此错误?:线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:3 - How do I fix this error?: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 如何在Android中修复ArrayIndexOutOfBoundsException - how to fix ArrayIndexOutOfBoundsException in android 如何在此代码中修复ArrayIndexOutOfBoundsException? - How to fix ArrayIndexOutOfBoundsException in this code? Java程序中出现ArrayIndexOutOfBoundsException错误,我不知道如何解决 - ArrayIndexOutOfBoundsException error in Java program, and I don't know how to fix it 如何在Hashtable列表中转换ResultSet? - How do I convert a ResultSet in a List of Hashtable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM