gpt4 book ai didi

java - 绘制节点的图片以及它们指向的位置

转载 作者:行者123 更新时间:2023-12-02 09:29:40 25 4
gpt4 key购买 nike

我应该直观地解释在输入以下代码后四个节点是如何出现的:

public class Node
{

//These two lines are provided
public Object Data = null;
public Node Next = null;

public static void main(String[] args)
{
Node a = new Node();
Node b = new Node();
Node c = new Node();
Node d = new Node();

//These four lines must be used
b = a.Next;
//c = b.Next; //Gives NullPointer error
//b.Data = c.Next.Data; //Gives NullPointer error
c.Next = a;
}
}

从我迄今为止所做的工作来看,似乎:

  • A 指向一个 Node(不变)
  • B 变成空对象
  • C的节点指向A
  • D 指向一个 Node(不变)

( This is the image 这是我从调试器中引用的图像)

上面两行给出NullPointer错误正常吗?我的猜测和图片有点接近吗?感谢您的帮助

最佳答案

鉴于每个 Node 对象的 DataNext 为空,让我们从您的主功能。 (建议您使用驼峰命名法来命名这些变量 https://en.wikipedia.org/wiki/Camel_case )

Node A = new Node(); 
Node B = new Node();
Node C = new Node();
Node D = new Node(); // Defines non-null A, B, C, and D Nodes

B = A.Next; // B = null; because the Next and Data of each node is null

C = B.Next; // C = (a non existent) b.next causing a null pointer error
B.Data = C.Next.Data; // c.next == null. null.Data doesn't exist.

C.Next = A; // C.Next = A; A == new Node(); no error

关于java - 绘制节点的图片以及它们指向的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com