Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在LinkedList中传递节点不会';不要打印任何东西_Java_Linked List - Fatal编程技术网

Java 在LinkedList中传递节点不会';不要打印任何东西

Java 在LinkedList中传递节点不会';不要打印任何东西,java,linked-list,Java,Linked List,我在add()方法中将head作为参数传递。我正在调用publicstaticvoidmain(stringargs[])中的add()方法。它不打印任何东西。但是,如果我没有将head作为参数传递,那么它将打印链接列表。为什么会这样 不打印任何内容的代码: public class linkedListI { static Node head; static class Node { int data;

我在
add()
方法中将
head
作为参数传递。我正在调用
publicstaticvoidmain(stringargs[])
中的
add()
方法。它不打印任何东西。但是,如果我没有将
head
作为参数传递,那么它将打印链接列表。为什么会这样

不打印任何内容的代码:

 public class linkedListI {
        static Node head;
    
        static class Node {
            int data;
            Node next;
    
            Node(int data) {
                this.data = data;
            }
        }
    
        static void add(Node head, int data) {
            Node new_node = new Node(data);
    
            if (head == null) {
    
                head = new_node;
                head.next = null;
            }
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new_node;
            new_node.next = null;
    
        }
    
        static void printLinkedList(Node head) {
            Node curr = head;
            while (curr != null) {
                System.out.print(curr.data + " ");
                curr = curr.next;
            }
        }
    
        public static void main(String args[]) {
            // linkedListI l = new linkedListI();
            add(head, 3);
            add(head, 4);
            add(head, 6);
            add(head, 7);
            printLinkedList(head);
        }
    }
给出输出的代码:

此代码打印输出,但我没有在
add()
方法中将
head
作为参数传递

public class linkedListI {
    static Node head;

    static class Node {
        int data;
        Node next;

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

    static void add(int data) {
        Node new_node = new Node(data);

        if (head == null) {

            head = new_node;
            head.next = null;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new_node;
        new_node.next = null;

    }

    static void printLinkedList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
    }

    public static void main(String args[]) {
        // linkedListI l = new linkedListI();
        add(3);
        add(4);
        add(6);
        add(7);
        printLinkedList(head);
    }
}

原因是在您的第一个版本中,您有:

static void add(Node head, int data)
这意味着此函数中对
head
的任何引用都将是局部变量,而不是在脚本顶部定义的类成员

由于参数是一个局部变量,当您为其指定一个新值时,没有人知道它,只有函数的执行上下文知道。调用代码在
头中看不到任何更改,因为参数是按值传递的。因此,
head
在每次调用
add
后将保持
null

如果希望它以这种方式工作—将
head
作为参数传递—然后更改
add
函数的签名,以便它将
head
的最终值返回给调用者,调用者必须将其重新分配回自己的
head
变量:


    static void add(Node head, int data) {
        Node new_node = new Node(data);

        new_node.next = null;
        if (head == null) {
            return new_node; // this is the new head
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new_node;
        return head; // head did not change
    }

// ....

    public static void main(String args[]) {
        head = add(head, 3);
        head = add(head, 4);
        head = add(head, 6);
        head = add(head, 7);
        printLinkedList(head);
    }

原因是在您的第一个版本中,您有:

static void add(Node head, int data)
这意味着此函数中对
head
的任何引用都将是局部变量,而不是在脚本顶部定义的类成员

由于参数是一个局部变量,当您为其指定一个新值时,没有人知道它,只有函数的执行上下文知道。调用代码在
头中看不到任何更改,因为参数是按值传递的。因此,
head
在每次调用
add
后将保持
null

如果希望它以这种方式工作—将
head
作为参数传递—然后更改
add
函数的签名,以便它将
head
的最终值返回给调用者,调用者必须将其重新分配回自己的
head
变量:


    static void add(Node head, int data) {
        Node new_node = new Node(data);

        new_node.next = null;
        if (head == null) {
            return new_node; // this is the new head
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new_node;
        return head; // head did not change
    }

// ....

    public static void main(String args[]) {
        head = add(head, 3);
        head = add(head, 4);
        head = add(head, 6);
        head = add(head, 7);
        printLinkedList(head);
    }

只是一个查询,但局部变量没有使用static关键字定义。我已将head设置为static。是的,您的查询是什么?只是一个查询,但局部变量没有使用static关键字定义。我已将head设置为static。是的,您的查询是什么?