我想知道是否有人可以帮助我尝试运行测试器类时发生的ArrayIndexOutOfBoundsException。
该异常在哈希表文件中的remove方法处发生。我试图用我朋友的代码替换掉我的代码,但是那也不起作用。
任何帮助将不胜感激
================================================ ==========================
这里是哈希表:
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 "";
}
}
}
这里是哈希表测试器:
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);
}
}
}
================================================ ==========================
在下面的共享Google驱动器链接中,您将找到一个包含数据输入的zip。
https://drive.google.com/file/d/1iYrzWl9mtv_io3q7K1_m2EtPFUXGbC3p/view?usp=sharing
您的问题在此位置:
while (index < hTable.length) {
index++;
if (hTable[index] == null)...
在最后一次迭代中,index
将与hTable.length
相同。