以下是我的代码的一部分。 对于 band 和 bandRows 的某些值,代码似乎运行得很好。但对于某些人来说,它会给出 ArrayIndexOutOfBounds 异常。 有什么想法我可能出错了吗?我在代码中找不到任何错误。 提前致谢
for(int i=0; i<bands; i++)
{
int a=0;
while(a<bucketSize)
{
bandBuckets[i][a] = new ArrayList();
a++;
}
}
for (int i = 0; i < bands; i++)
{
for (int j = 0; j < preprocessedList.size(); j++)
{
int[][] forBuckets = new int[bands][bandRows];
for (int k = 0; k < bandRows; k++)
{
Arrays.fill(forBuckets[i], Bands[i][k][j]);
}
bandBuckets[i][h.hashBands(forBuckets[i], bucketSize)].add(j);
}
}
这是另一个类中的 h.hashBands() 函数
public int hashBands(int[] in, int bucketSize)
{
int hashVal = 0;
int k = in.length;
int base = 3;
for (int i = 0; i < in.length; i++) {
// for (int j = 0; i < in[i].length; i++)
hashVal += in[i] * Math.pow(base, k - i - 1);
}
return hashVal % bucketSize;
}
最佳答案
也许您的 hashBands() 函数存在溢出。
int 的最大值为 231 - 1。当 k - i - 1 大于 19 时,hashVal 将溢出。在 Java 中,上溢和下溢不会引发异常。考虑使用 BigInteger 及其 modPow() 函数。
关于java - 某些输入值的 ArrayIndexOutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20087116/