传递参考值不适用于java

时间:2017-07-15 13:34:43

标签: java pass-by-reference quicksort singly-linked-list pass-by-value

我正在尝试使用java中的单链表为quicksort编写程序。

以下是代码。

public class QuickSortInSLinkedList {
Node head;
private static class Node{
    private int data;
    private Node next;

    Node(int data){
        this.data = data;
        this.next = null;
    }
}


public void printList(Node head){
    Node node = head;
    while(node != null){
        System.out.print(node.data+" ,");
        node = node.next;
    }
}

private Node getLastNode(Node head){
    Node node = head;
    while(node != null && node.next != null){
        node = node.next;
    }
    return node;
}

public void push(int data){
    Node node = new Node(data);

    if(head == null){
        head = node;
        return;
    }

    node.next = head;
    head = node;
}

void quickSort(Node head){
    Node lastnode = getLastNode(head);
    head = _quickSort(head, lastnode);
    return;
}

Node _quickSort(Node low, Node high){
    Node newHead = null, newTail = null;

    if(low == null || low == high){
        return low;
    }

    Node part = partition(low, high, newHead, newTail);

    if (newHead != part){
        Node temp = newHead;
        while(temp.next != part){
            temp = temp.next;
        }

        temp.next = null;

        newHead = _quickSort(newHead, temp);
        temp = getLastNode(newHead);
        temp.next = part;

    }

    part.next = _quickSort(part.next, newTail);
    return newHead;
}

private Node partition(Node low, Node high, Node newHead, Node newTail){
    Node pivot = high;
    Node previous = null, current = head, tail = pivot;

    while(current != pivot){
        if (current.data < pivot.data){
            if (newHead == null)
                newHead = current;

            previous = current;
            current = current.next;
        }else{
            if(previous != null)
                previous.next = current.next;

            Node temp = current.next;
            current.next = null;
            tail.next = current;
            tail = current;
            current = temp;
        }
    }

    if(newHead == null){
        newHead = pivot;
    }

    newTail = tail;

    return pivot;
}

public static void main(String[] args){
    QuickSortInSLinkedList list = new QuickSortInSLinkedList();
    list.push(5);
    list.push(35);
    list.push(7);
    list.push(8);
    list.push(34);
    list.push(23);

    System.out.println("Linked list before sorting");
    list.printList(list.head);

    System.out.println("\n Linked list after sorting");
    list.quickSort(list.head);
    list.printList(list.head);

}

}

我理解,因为在java中我们通过引用值传递,所以这段代码应该可以工作但是在第62行,即变量newHead和newTail在调用分区方法之后总是被接收为null。

以下是错误

线程“main”java.lang.NullPointerException中的异常 23,34,8,7,35,5,     at implementation.sorting.QuickSortInSLinkedList $ Node.access $ 100(QuickSortInSLinkedList.java:6)  排序后的链接列表     at implementation.sorting.QuickSortInSLinkedList._quickSort(QuickSortInSLinkedList.java:62)     at implementation.sorting.QuickSortInSLinkedList.quickSort(QuickSortInSLinkedList.java:47)     at implementation.sorting.QuickSortInSLinkedList.main(QuickSortInSLinkedList.java:123)

请帮助我理解为什么会如此。 感谢

1 个答案:

答案 0 :(得分:0)

Java通过引用操作对象,所有对象变量都是引用。但是,Java并没有通过引用传递方法参数;它按值传递它们。