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。是的,您的查询是什么?