我在线程“main”中有例外。 java.lang.arrayindexoutofboundsexception:-47我的作业错误 [英] I am having exception in thread "main" java.lang.arrayindexoutofboundsexception: -47 error in my homework
问题描述
我的代码是:
my code is:
package code;
import java.lang.reflect.Array;
import java.util.LinkedList;
import given.AbstractHashMap;
import given.HashEntry;
import given.iPrintable;
/*
* The file should contain the implementation of a hashmap with:
* - Open addressing for collision handling
* - Double hashing for probing. The double hash function should be of the form: q - (k mod q)
* - Multiply-Add-Divide (MAD) for compression: (a*k+b) mod p
* - Resizing (to double its size) and rehashing when the load factor gets above a threshold
*
* Some helper functions are provided to you. We suggest that you go over them.
*
* You are not allowed to use any existing java data structures other than for the keyset method
*/
public class HashMapDH<Key, Value> extends AbstractHashMap<Key, Value> {
// The underlying array to hold hash entries (see the HashEntry class)
private HashEntry<Key, Value>[] buckets;
@SuppressWarnings("unchecked")
protected void resizeBuckets(int newSize) {
// Update the capacity
N = nextPrime(newSize);
buckets = (HashEntry<Key, Value>[]) Array.newInstance(HashEntry.class, N);
}
// The threshold of the load factor for resizing
protected float criticalLoadFactor;
// The prime number for the secondary hash
int dhP;
/*
* ADD MORE FIELDS IF NEEDED
*
*/
/*
* ADD A NESTED CLASS IF NEEDED
*
*/
// Default constructor
public HashMapDH() {
this(101);
}
public HashMapDH(int initSize) {
this(initSize, 0.6f);
}
public HashMapDH(int initSize, float criticalAlpha) {
N = initSize;
criticalLoadFactor = criticalAlpha;
resizeBuckets(N);
// Set up the MAD compression and secondary hash parameters
updateHashParams();
/*
* ADD MORE CODE IF NEEDED
*
*/
}
/*
* ADD MORE METHODS IF NEEDED
*
*/
/**
* Calculates the hash value by compressing the given hashcode. Note that you
* need to use the Multiple-Add-Divide method. The class variables "a" is the
* scale, "b" is the shift, "mainP" is the prime which are calculated for you.
* Do not include the size of the array here
*
* Make sure to include the absolute value since there maybe integer overflow!
*/
protected int primaryHash(int hashCode) {
// TODO: Implement MAD compression given the hash code, should be 1 line
return (hashCode*a+b)%P % N;
}
/**
* The secondary hash function. Remember you need to use "dhP" here!
*
*/
protected int secondaryHash(int hashCode) {
// TODO: Implement the secondary hash function taught in the class
return dhP-hashCode%dhP;
}
@Override
public int hashValue(Key key, int iter) {
int k = Math.abs(key.hashCode());
return Math.abs(primaryHash(k) + iter * secondaryHash(k)) % N;
}
/**
* checkAndResize checks whether the current load factor is greater than the
* specified critical load factor. If it is, the table size should be increased
* to 2*N and recreate the hash table for the keys (rehashing). Do not forget to
* re-calculate the hash parameters and do not forget to re-populate the new
* array!
*/
protected void checkAndResize() {
if (loadFactor() > criticalLoadFactor) {
// TODO: Fill this yourself
int oldn=n;
updateHashParams();
HashEntry<Key, Value>[] temp=buckets;
resizeBuckets(2*N);
for(int i=0;i<temp.length;i++) {
buckets[i]=temp[i];
}
n=oldn;
}
}
@Override
public Value get(Key k) {
// TODO Auto-generated method stub
int hash1 = primaryHash(k.hashCode() );
int hash2 = secondaryHash(k.hashCode() );
while (buckets[hash1] != null && !buckets[hash1].getKey().equals(k))
{
hash1 += hash2;
hash1 %= N;
}
return buckets[hash1].getValue();
}
@Override
public Value put(Key k, Value v) {
// TODO Auto-generated method stub
// Do not forget to resize if needed!
if(n==N) {
checkAndResize();
}
int hash1=primaryHash(k.hashCode());
int hash2=secondaryHash(k.hashCode());
while(buckets[hash1]!=null) {
hash1+=hash2;
hash1%=N;
}
buckets[hash1]=new HashEntry<Key,Value>(k,v);
n++;
return buckets[hash1].getValue();
}
@Override
public Value remove(Key k) {
// TODO Auto-generated method stub
int hash1=primaryHash(k.hashCode());
int hash2=secondaryHash(k.hashCode());
while(buckets[hash1]!=null && !buckets[hash1].getKey().equals(k)) {
hash1 += hash2;
hash1 %= N;
}
HashEntry<Key, Value> temp=new HashEntry<Key,Value>(buckets[hash1].getKey(),buckets[hash1].getValue());
buckets[hash1]=null;
n--;
return temp.getValue();
}
// This is the only function you are allowed to use an existing Java data
// structure!
@Override
public Iterable<key> keySet() {
// TODO Auto-generated method stub
LinkedList<key> r=new LinkedList<key>();
for(int i=0;i<buckets.length;i++) {
HashEntry<Key,Value> e;
e=buckets[i];
if(e!=null) {
r.add(e.getKey());
}
}
return r;
}
@Override
protected void updateHashParams() {
super.updateHashParams();
dhP = nextPrime(N / 2);
}
}
我也有这些错误:
I also having these errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -47
at code.HashMapDH.get(HashMapDH.java:136)
at given.TestHashMap.testGet(TestHashMap.java:119)
at given.TestHashMap.testAll(TestHashMap.java:246)
at given.Test.main(Test.java:464)
code.HashMapDh.get(HashMapDH.java:136)第136行的错误是get方法的while循环。我不知道如何解决它请帮帮我
我尝试过的事情:
这是我家庭工作的一部分。我无法解决这些错误,请你帮忙吗?它给出了错误
the error at code.HashMapDh.get(HashMapDH.java:136) line 136 is the get methods' while loop. I dont know how to fix it please help me
What I have tried:
It is part of my home work. I couldn't fix the errors could you please help me? it is giving the errors
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -47
at code.HashMapDH.get(HashMapDH.java:136)
at given.TestHashMap.testGet(TestHashMap.java:119)
at given.TestHashMap.testAll(TestHashMap.java:246)
at given.Test.main(Test.java:464)
推荐答案
仔细查看错误信息:它为您提供了大量信息!
Look at the error message carefully: it gives you a lot of information!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -47
表示您已尝试访问不存在的数组元素。例如,您尝试使用负数组索引,或者数组有四个元素,您尝试使用4或更大的索引(Javea中的数组基于0,因此有效索引将为0,1 ,2和3只)。
错误消息的下一位告诉你它出现在哪里:
Is saying that you have tried to access an element of an array that doesn't exist. For example, you have tried to use a negative array index, or of the array had four elements, you have tried to use an index that is 4 or larger (arrays in Javea are 0 based, so the valid indexes would be 0, 1, 2, and 3 only).
The next bit of the error message tells you where it occurred:
at code.HashMapDH.get(HashMapDH.java:136)
该文件为HashMapDH.java,行号为136,错误在函数get中
首先找出哪一行是行号136(在IDE中尝试CTRL + G,这通常会直接转到行号),然后使用调试器找出实际的内容数组索引是,你的数组有多大。
很抱歉,但我们不能为你做到这一点 - 我们无法使用相同的数据运行你的代码 - 时间让你学习一门新的(非常非常有用的)技能:调试!
The file is "HashMapDH.java", the line number is "136" and the error is in the function "get"
So start by finding out which line is "Line number 136" (try CTRL+G in your IDE, that often takes you directly to a line number) and then use the debugger to find out what the actual array indexes are, and how big your array is.
Sorry, but we can't do that for you - we can't run your code with the same data - time for you to learn a new (and very, very useful) skill: debugging!
这篇关于我在线程“main”中有例外。 java.lang.arrayindexoutofboundsexception:-47我的作业错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!