簡體   English   中英

為什么它將索引超出范圍拋出異常?

[英]why it throws index out of bounds exception?

我想使用合並排序對我的雙向鏈表進行排序。我已經創建了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類:(為了更好地理解,我把整個代碼都放在了這個類中)

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)

請多多幫助我。

這是一個非常規的“答案”-接下來的幾個小時我將在這里,所以我將嘗試引導您完成查找和修復此bug的過程。

步驟1.無法捕獲異常中的信息

此檢查在getNodes引發了異常:

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

為了簡化調試,您應該始終提供有關異常的信息(請參閱: 有效的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));
}

這很可能是罪魁禍首。 當您嘗試在索引x處添加元素時, listTwo為空。 也許你想要這樣的東西:

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來提高可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM