【问题标题】:Pass by reference value not working in java [duplicate]通过引用值传递在java中不起作用[重复]
【发布时间】:2017-12-20 11:45:08
【问题描述】:

我正在尝试使用 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);

}

}

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

下面是错误

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

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

【问题讨论】:

  • Java 严格来说是pass-by-value, not pass-by-reference
  • 我浏览了这个链接stackoverflow.com/questions/5298421/…,它提到-“它按值传递对象引用。因为同一个引用的两个副本引用同一个实际对象,所以通过一个引用变量所做的更改是可见的通过另一个。” ,我很困惑 newHead 也应该被修改?
  • 是的,你很困惑。如果参数是对象,则不会复制该对象,并且对对象的字段的任何更改都将在方法之外看到。但是参数本身不能改变——你不能分配给参数(你可以,但它不会在方法之外看到),你只能修改它所引用的对象中的字段.
  • 感谢@RealSkeptic

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 2011-08-28
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2011-02-24
    相关资源
    最近更新 更多