私はquicksortのプログラムをJavaで単独でリンクされたリストを使って書こうとしています。参照値渡しで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で、我々は、基準値によってパスを持っているので、このコードは動作しますが、変数newHeadとnewTailは常にパーティションメソッドの呼び出しの後にヌルとして受信される、すなわちライン62でなければならないことを理解します。以下
スレッドにエラー
例外 "メイン" java.lang.NullPointerExceptionが 23、34、8、7、35、5、 implementation.sorting.QuickSortInSLinkedList $ Node.access $ 100℃で(QuickSortInSLinkedListあります.java:implementation.sorting.QuickSortInSLinkedListでimplementation.sorting.QuickSortInSLinkedList.quickSort(QuickSortInSLinkedList.java:47) でimplementation.sorting.QuickSortInSLinkedList._quickSort(QuickSortInSLinkedList.java:62) でソートした後6) リンクされたリスト。メイン(QuickSortInSLinkedList.java:123)
私はそれがなぜそう理解するのを助けてください。 ありがとう
Javaは厳密に[パス渡しで、参照渡しではありません](http://stackoverflow.com/q/40480/4125191)です。 – RealSkeptic
私はこのリンクを見ました。https://stackoverflow.com/questions/5298421/why-doesnt-java-support-pass-by-reference-like-c、それは言います - 「オブジェクト参照を値渡しします。同じ参照の同じ実際のオブジェクトを参照して、1つの参照変数を介して行われた変更は、他の参照変数を介して表示されます。 、私はnewHeadも変更される必要がありますように混乱している? – Astlez
はい、あなたは混乱しています。引数がオブジェクトの場合、オブジェクトは複製されず、**オブジェクトの**フィールドへの変更はメソッドの外に表示されます。しかし、引数*自体は変更できません。引数に代入することはできません(ただし、メソッドの外側には表示されません)。参照するオブジェクトのフィールドのみを変更できます。 – RealSkeptic