为什么它抛出索引越界异常?

时间:2010-05-30 12:48:24

标签: java linked-list

我想使用合并排序来对我的双向链表进行排序。我创建了3个类(Node,DoublyLinkedList,MergeSort),但它会为这些行抛出此异常:

1.in the getNodes method of DoublyLinkedList--->   throw new IndexOutOfBoundsException();
2.in the add method of DoublyLinkedList----->   Node cursor = getNodes(index);
3.in the sort method of MergeSort class------>    listTwo.add(x,localDoublyLinkedList.getValue(x));
4.in the main method of DoublyLinkedList----->merge.sort();

这是我的Merge类:(我把这个类的整个代码放在了解beter中)

public class MergeSort {

private DoublyLinkedList localDoublyLinkedList;

public MergeSort(DoublyLinkedList list) {
    localDoublyLinkedList = list;

}

public void sort() {

    if (localDoublyLinkedList.size() <= 1) {
        return;
    }
    DoublyLinkedList listOne = new DoublyLinkedList();
    DoublyLinkedList listTwo = new DoublyLinkedList();
    for (int x = 0; x < (localDoublyLinkedList.size() / 2); x++) {
        listOne.add(x, localDoublyLinkedList.getValue(x));
    }
    for (int x = (localDoublyLinkedList.size() / 2) + 1; x < localDoublyLinkedList.size(); x++) {
        listTwo.add(x, localDoublyLinkedList.getValue(x));
    }
//Split the DoublyLinkedList again
        MergeSort sort1 = new MergeSort(listOne);
        MergeSort sort2 = new MergeSort(listTwo);
        sort1.sort();
        sort2.sort();

    merge(listOne, listTwo);
}

public void merge(DoublyLinkedList a, DoublyLinkedList b) {
    int x = 0;
    int y = 0;
    int z = 0;
    while (x < a.size() && y < b.size()) {
        if (a.getValue(x) < b.getValue(y)) {
            localDoublyLinkedList.add(z, a.getValue(x));
            x++;
        } else {
            localDoublyLinkedList.add(z, b.getValue(y));
            y++;
        }
        z++;
    }
    //copy remaining elements to the tail of a[];
    for (int i = x; i < a.size(); i++) {
        localDoublyLinkedList.add(z, a.getValue(i));
        z++;
    }

    for (int i = y; i < b.size(); i++) {
        localDoublyLinkedList.add(z, b.getValue(i));
        z++;
    }

}
}

只是我的DoublyLinkedList的一部分:

    private Node getNodes(int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > length) {
        throw new IndexOutOfBoundsException();
    } else {
        Node cursor = head;
        for (int i = 0; i < index; i++) {
            cursor = cursor.getNext();
        }
        return cursor;
    }
}
  public void add(int index, int value) throws IndexOutOfBoundsException {
    Node cursor = getNodes(index);
    Node temp = new Node(value);
    temp.setPrev(cursor);
    temp.setNext(cursor.getNext());
    cursor.getNext().setPrev(temp);
    cursor.setNext(temp);
    length++;
}

    public static void main(String[] args) {
    int i = 0;
    i = getRandomNumber(10, 10000);
    DoublyLinkedList list = new DoublyLinkedList();
    for (int j = 0; j < i; j++) {
        list.add(j, getRandomNumber(10, 10000));
        MergeSort merge = new MergeSort(list);
        merge.sort();

        System.out.println(list.getValue(j));
    }
}

也是堆栈跟踪:

run:
0
0
Exception in thread "main" java.lang.IndexOutOfBoundsException
    at datastructureproject.DoublyLinkedList.getNodes(DoublyLinkedList.java:57)
    at datastructureproject.DoublyLinkedList.add(DoublyLinkedList.java:34)
    at datastructureproject.MergeSort.sort(MergeSort.java:31)
    at datastructureproject.DoublyLinkedList.main(DoublyLinkedList.java:82)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

当我调试时:

debug:
0
0
Exception in thread "main" java.lang.IndexOutOfBoundsException
    at datastructureproject.DoublyLinkedList.getNodes(DoublyLinkedList.java:57)
    at datastructureproject.DoublyLinkedList.add(DoublyLinkedList.java:34)
    at datastructureproject.MergeSort.sort(MergeSort.java:31)
    at datastructureproject.DoublyLinkedList.main(DoublyLinkedList.java:82)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

请多多帮助我。

1 个答案:

答案 0 :(得分:2)

这是一个非常规的“答案” - 我将在接下来的几个小时内来到这里,所以我将尽力引导你找到并修复这个错误。

步骤1.在异常中捕获信息失败

此检查在getNodes中抛出异常:

if (index < 0 || index > length) {
   throw new IndexOutOfBoundsException();

为了简化调试,您应始终提供异常信息(请参阅: Effective Java 2nd Edition:第63项:在详细消息中包含故障捕获信息)。帮助我们识别和修复此错误的第一步就是:

if (index < 0 || index > length) {
   throw new IndexOutOfBoundsException("Index=" + index + "; length=" + length);

现在,每当抛出异常时,我们都会收到indexlength的值的详细信息。


中场休息:怀疑#1

DoublyLinkedList listTwo = new DoublyLinkedList();
for (int x = (localDoublyLinkedList.size() / 2) + 1;
        x < localDoublyLinkedList.size(); x++) {
    listTwo.add(x, localDoublyLinkedList.getValue(x));
}

很有可能,这是罪魁祸首。当您尝试在索引listTwo添加元素时,x为空。也许你想要这样的东西:

DoublyLinkedList listTwo = new DoublyLinkedList();
for (int x = (localDoublyLinkedList.size() / 2) + 1, offset = x;
        x < localDoublyLinkedList.size(); x++) {
    listTwo.add(x - offset, localDoublyLinkedList.getValue(x));
}

这使用offset计算,以便listTwo.add从索引0开始。


间歇#2:使用更多final局部变量

我建议有这样的事情:

final int L = localDoublyLinkedList.size();
final int M = L / 2;

然后在算法中使用LM来提高可读性。