Java 找不到数组越界异常

Java 找不到数组越界异常,java,arrays,bounds,out,Java,Arrays,Bounds,Out,我一直在努力寻找越界异常的来源,这让我发疯!只有当我的HEAPPERIORITYQUEUE通过下面的压力测试时,它才会出现。 当我的代码通过此压力测试运行时,基本上会出现数组越界异常: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 110 at HeapPriorityQueue.hasLeft(HeapPriorityQueue.java:168) at HeapPriorityQu

我一直在努力寻找越界异常的来源,这让我发疯!只有当我的HEAPPERIORITYQUEUE通过下面的压力测试时,它才会出现。 当我的代码通过此压力测试运行时,基本上会出现数组越界异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 110 

    at HeapPriorityQueue.hasLeft(HeapPriorityQueue.java:168)
    at HeapPriorityQueue.bubbleDown(HeapPriorityQueue.java:111)
    at HeapPriorityQueue.removeMin(HeapPriorityQueue.java:73)
    at a4tester.testRandomArray(a4tester.java:224)
    at a4tester.stressTest(a4tester.java:237)
    at a4tester.main(a4tester.java:283)

}有几件事可能会有问题:

public class HeapPriorityQueue implements PriorityQueue {
protected final static int DEFAULT_SIZE = 10000;

/* This array is where you will store the elements in the heap */
protected Comparable storage[];

/* Keep track of the current number of elements in the heap */
protected int currentSize;

/* You do not need to change this constructor */
public HeapPriorityQueue () 
{
    this(DEFAULT_SIZE);
}

/* You do not need to change this constructor */
public HeapPriorityQueue(int size)
{
    storage = new Comparable[size + 1];
    currentSize = 0;
}

/*
 * You need to change the implementation of every public method
 * below this comment.
 *
 */
public int size () {
    return currentSize;
}

public boolean isEmpty () {
    if(size() == 0)
        return true;
    return false;
}

public Comparable removeMin () throws HeapEmptyException {
    if(isEmpty())
        throw new HeapEmptyException();
    Comparable returnValue = storage[1];
    storage[1] = storage[currentSize];
    storage[currentSize] = null;
    currentSize--;
    bubbleDown();
    return returnValue;
}

public void insert ( Comparable k  ) throws HeapFullException {
    if(currentSize >= storage.length - 1)
        throw new HeapFullException();
    currentSize++;
    storage[currentSize] = k;
    bubbleUp();

}

/* Your instructor's solution used the following helper methods
 * 
 * You do not need to use the same methods, but you may want to.
 */

/* 
 * A new value has just been added to the bottom of the heap
 * "bubble up" until it is in the correct position
 */
private void bubbleUp () {
    int index = currentSize;
    while(parent(index) != 0 && storage[parent(index)].compareTo(storage[index]) > 0) {
        swapElement(index, parent(index));
        index = parent(index);
    }
}

/*
 * Because of a removeMin operation, a value from the bottom
 * of the heap has been moved to the root.
 * 
 * "bubble down" until it is in the right position
 */
private void bubbleDown() {  
    int index = 1;   
    while (hasLeft(index)) {    
        int sc = leftChild(index);  
        if (hasRight(index) && storage[leftChild(index)].compareTo(storage[rightChild(index)]) > 0) {  
            sc = rightChild(index);  
        }   
        if (storage[index].compareTo(storage[sc]) > 0) {  
            swapElement(index, sc);  
        } 
        else{
        }
        index = sc;  
    }          
}

/*
 * Swap the element at position p1 in the array with the element at 
 * position p2
 */
private void swapElement ( int p1, int p2 ) {
    Comparable temp = storage[p1];
    storage[p1] = storage[p2];
    storage[p2] = temp;
}

/*
 * Return the index of the parent of the node at pos
 */
private int parent ( int pos )
{
    return (pos/2); // replace this with working code
}

/* 
 * Return the index of the left child of the node at pos
 */
private int leftChild ( int pos )
{
    return (pos*2); // replace this with working code
}

/* 
 * Return the index of the right child of the node at pos
 */
private int rightChild ( int pos )
{   
    return (pos * 2)+1; // replace this with working code
}

/*
 * Given the current number of elements in the heap, does the
 * node at pos have a left child?
 *
 * Note that all internal nodes have at least a left child.
 *
 */
private boolean hasLeft ( int pos )
{
    if(storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

/*
 * Given the current number of elements in the heap, does the
 * node at pos have a right child?
 */ 
private boolean hasRight ( int pos ) {
    if(storage[rightChild(pos)] != null)
        return true;
    return false; // replace this with working code
}
应更改为以下内容:

private boolean hasLeft ( int pos )
{
    if(storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

因为您可能会在原始代码中越界。同样的逻辑也适用于
hasRight()
。此外,您还试图访问
存储
,但从未检查其长度,即您有
存储[1]
,但不能保证您没有将0传递给构造函数。希望能有所帮助。

请确保使用显示行号的编辑器。这样就很容易找到故障线路。那么,什么是168线???错误发生在第168行。知道这是哪一行至少可以很容易地看到异常的原因。如果没有这些信息,你会强迫你的读者猜测。是的,我只是注意到Hasleet没有检查左边孩子的索引是否超出范围。一切都解决了。谢谢大家!@A没问题。既然你是SO的新手,那就去看看吧。
private boolean hasLeft ( int pos )
{
    if(storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}
private boolean hasLeft ( int pos )
{
    if (storage.length > leftChild(pos) && storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}