//我有一个Node.java类
public class Node{
int data;
Node next;
public Node(int d) {
data = d;
}
}
//另一个java类
class LinkedList {
Node head;
public static void main(String[] args) {
LinkedList list = new LinkedList();
//Executing this loop
for (int i = 0; i < 5; i++) {
**list.add(i);**
}
}
void add(int value){
Node newNode = new Node(value);
if(head == null )//Very first time its create the head object when i = 0
{
head = newNode;
}else if(head.next == null){//This is for when i value is 1
head.next = newNode;
}else{ //else part execute i >= 2
//Created new node with head.next which mean value 1.And head is 0
Node temp = head.next;
// Just need this object initialization for reference
Node temp1 = newNode;
//Checking head.next is null or not if its null skip this loop execution
while(temp != null)
{
temp1 = temp;
temp = temp.next;
}
// Here we set newNode.next to null
newNode.next = temp1.next;
temp1.next = newNode;
}
}
}
我的问题在这里,当temp1.next = newNode;行执行头对象已添加 下一个值。
** //例如,如果head = 0,则当temp1.next = newNode时head.next = 1; line execute head.next.next = 2正在添加head。当我们没有头对象引用时它是如何发生的。
答案 0 :(得分:1)
您没有更新头部对象。
您正在更新head.next
对象。
所以
head.next.next
可以这样写:
Node nextFromHead = head.next; // nextFromHead is 1
Node nextFromNextFromHead = nextFromHead.next; // nextFromNextFromHead is 2
head.next.next
与nextFromNextFromHead
是同一个对象,但它(2为节点)与头节点没有任何直接连接。
我认为这有助于更好地理解java中的引用如何工作。
public class LinkedList {
static Node head;
public static void main(String[] args) {
LinkedList list = new LinkedList();
for(int i = 0; i < 5; i++)
list.add(i);
Node currentNode = head; // in java we don't need object initialization for reference. Node temp1; would work just fine
System.out.println("==head node== " + currentNode);
while(currentNode.next != null) {
// here we increment
currentNode = currentNode.next;
// System.out.println("Last time we in here, next is null so print only current");
System.out.println("==next node== " + currentNode);
}
}
void add(int value){
Node newNode = new Node(value);
if(head == null )//Very first time its create the head object when i = 0
{
head = newNode;
}else if(head.next == null){//This is for when i value is 1
head.next = newNode;
}else{ //else part execute i >= 2
//Created new node with head.next which mean value 1.And head is 0
Node temp = head.next;
// Just need this object initialization for reference
Node temp1 = newNode;
//Checking head.next is null or not if its null skip this loop execution
while(temp != null)
{
temp1 = temp;
temp = temp.next;
}
// Here we set newNode.next to null
System.out.println(" ==temp1== " + temp1);// before
newNode.next = temp1.next;
temp1.next = newNode;
System.out.println(" ==temp1== " + temp1);// and after
}
System.out.println("==current node== " + head);
System.out.println();
}
}
Node类带有一个额外的toString(),用于正确查看对象。
public class Node {
int data;
Node next;
public Node(int d) {
data = d;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
", next=" + next +
'}';
}
}
答案 1 :(得分:0)
“你”确实有 head 元素。
看看你的代码:你的LinkedList类有一个字段头;每当你调用列表的add()方法时;该方法可以访问该字段。
所以,添加这样的作品:
这就是要了解的全部内容。或者让我们尝试一些非IT示例。
假设你有一些钩子和短绳;并且你想建立一个“绳索列表”。
希望有所帮助。