[英]why it throws index out of bounds exception?
I want to use merge sort for sorting my doubly linked list.I have created 3 classes(Node,DoublyLinkedList,MergeSort) but it will throw this exception for these lines: 我想使用合并排序对我的双向链表进行排序。我已经创建了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();
this is my Merge class:(I put the whole code for this class for beter understanding) 这是我的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++;
}
}
}
and just a part of my DoublyLinkedList: 只是我的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));
}
}
also thiese are the stacktrace: 也就是堆栈跟踪:
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)
also when I debug: 当我调试时:
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)
PLEASE help me thanks alot. 请多多帮助我。
This is an unconventional "answer" -- I will be here for the next few hours, so I will try to guide you along the process of finding and fixing this bug. 这是一个非常规的“答案”-接下来的几个小时我将在这里,所以我将尝试引导您完成查找和修复此bug的过程。
The exception is thrown in getNodes
by this check: 此检查在
getNodes
引发了异常:
if (index < 0 || index > length) {
throw new IndexOutOfBoundsException();
To ease debugging, you should always provide information for your exceptions (see: Effective Java 2nd Edition: Item 63: Include failure capture information in detail messages ). 为了简化调试,您应该始终提供有关异常的信息(请参阅: 有效的Java 2nd Edition:项目63:在详细消息中包括故障捕获信息 )。 The first step to help us identify and fix this bug is to do just that:
帮助我们识别和修复此错误的第一步就是这样做:
if (index < 0 || index > length) {
throw new IndexOutOfBoundsException("Index=" + index + "; length=" + length);
Now, whenever the exception is thrown, we also get a detailed message of what the values of index
and length
was. 现在,无论何时抛出异常,我们都会获得有关
index
和length
的值的详细信息。
DoublyLinkedList listTwo = new DoublyLinkedList();
for (int x = (localDoublyLinkedList.size() / 2) + 1;
x < localDoublyLinkedList.size(); x++) {
listTwo.add(x, localDoublyLinkedList.getValue(x));
}
In all likelihood, this is the culprit. 这很可能是罪魁祸首。
listTwo
is empty when you try to add element at index x
. 当您尝试在索引
x
处添加元素时, listTwo
为空。 Perhaps you want something like this: 也许你想要这样的东西:
DoublyLinkedList listTwo = new DoublyLinkedList();
for (int x = (localDoublyLinkedList.size() / 2) + 1, offset = x;
x < localDoublyLinkedList.size(); x++) {
listTwo.add(x - offset, localDoublyLinkedList.getValue(x));
}
This uses the offset
calculation so that listTwo.add
starts at index 0
. 这使用
offset
计算,以便listTwo.add
从索引0
开始。
final
local variables final
局部变量 I suggest having something like this: 我建议有这样的事情:
final int L = localDoublyLinkedList.size();
final int M = L / 2;
Then using L
and M
in your algorithms to improve readability. 然后在算法中使用
L
和M
来提高可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.