传递引用值在java中不起作用

传递引用值在java中不起作用,java,pass-by-reference,quicksort,singly-linked-list,pass-by-value,Java,Pass By Reference,Quicksort,Singly Linked List,Pass By Value,我正试图用java编写一个使用单链表的快速排序程序 下面是代码 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 pr

我正试图用java编写一个使用单链表的快速排序程序

下面是代码

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

}
公共类QuickSortInLinkedList{
节点头;
私有静态类节点{
私有int数据;
私有节点下一步;
节点(int数据){
这个数据=数据;
this.next=null;
}
}
公共作废打印列表(节点头){
节点=头部;
while(节点!=null){
系统输出打印(节点数据+“,”);
node=node.next;
}
}
私有节点getLastNode(节点头){
节点=头部;
while(node!=null&&node.next!=null){
node=node.next;
}
返回节点;
}
公共无效推送(整数数据){
节点=新节点(数据);
if(head==null){
头部=节点;
返回;
}
node.next=头部;
头部=节点;
}
无效快速排序(节点头){
节点lastnode=getLastNode(头部);
head=\u快速排序(head,lastnode);
返回;
}
节点_快速排序(节点低、节点高){
节点newHead=null,newTail=null;
如果(低==零| |低==高){
低回报;
}
节点部分=分区(低、高、新头、新尾);
if(新头!=零件){
节点温度=新头;
while(临时下一步!=零件){
温度=下一个温度;
}
temp.next=null;
newHead=_快速排序(newHead,temp);
temp=getLastNode(newHead);
温度下一个=零件;
}
part.next=\u快速排序(part.next,newTail);
返回newHead;
}
专用节点分区(节点低、节点高、节点新头、节点新尾){
节点轴=高;
节点前一个=null,当前=头部,尾部=枢轴;
while(当前!=轴){
if(当前数据<透视数据){
if(newHead==null)
新头=电流;
先前=当前;
当前=当前。下一步;
}否则{
如果(上一个!=null)
previous.next=current.next;
节点温度=当前。下一步;
current.next=null;
tail.next=当前;
尾=电流;
电流=温度;
}
}
if(newHead==null){
新头=枢轴;
}
新尾巴=尾巴;
返回轴;
}
公共静态void main(字符串[]args){
QuickSortInLinkedList=新建QuickSortInLinkedList();
列表。推送(5);
列表。推送(35);
列表。推送(7);
列表。推送(8);
列表。推送(34);
列表。推送(23);
System.out.println(“排序前的链表”);
列表.打印列表(列表.标题);
System.out.println(“\n排序后的链表”);
list.quickSort(list.head);
列表.打印列表(列表.标题);
}
}

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

下面是错误

线程“main”java.lang.NullPointerException中出现异常 23 ,34 ,8 ,7 ,35 ,5 , 在implementation.sorting.QuickSortInLinkedList$Node.access$100处(QuickSortInLinkedList.java:6) 排序后的链表 在implementation.sorting.QuickSortInLinkedList.\u quickSort(QuickSortInLinkedList.java:62) 位于implementation.sorting.QuickSortInLinkedList.quickSort(QuickSortInLinkedList.java:47) 位于implementation.sorting.QuickSortInLinkedList.main(QuickSortInLinkedList.java:123)

请帮助我理解为什么会这样。
谢谢

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

Java是严格意义上的。我浏览了这个链接,它提到“它通过值传递对象引用。因为同一引用的两个副本引用同一个实际对象,通过一个引用变量所做的更改通过另一个引用变量可见。”,我很困惑newHead也应该如何修改?是的,你很困惑。如果参数是对象,则不会复制该对象,并且对象字段的任何更改都将显示在方法之外。但是参数本身无法更改-您不能指定参数(您可以,但在方法之外看不到它),您只能修改它所引用的对象中的字段。谢谢@realpoint