why it throws index out of bounds exception??

Posted by Johanna on Stack Overflow See other posts from Stack Overflow or by Johanna
Published on 2010-05-30T12:48:23Z Indexed on 2010/05/30 12:52 UTC
Read the original article Hit count: 265

Filed under:
|

Hi 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:

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)

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:

    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));
    }
}

PLEASE help me thanks alot.

© Stack Overflow or respective owner

Related posts about java

Related posts about linked-list