gpt4 book ai didi

java - 为什么会抛出索引越界异常?

转载 作者:行者123 更新时间:2023-12-02 08:32:39 25 4
gpt4 key购买 nike

我想使用合并排序来对我的双向链表进行排序。我已经创建了 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();

这是我的合并类:(我放置了该类的完整代码以便更好地理解)

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.捕获异常信息失败

此检查在 getNodes 中引发异常:

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

为了简化调试,您应该始终提供异常信息(请参阅:Effective Java 第 2 版:第 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 来提高可读性。

关于java - 为什么会抛出索引越界异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2938683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com