java - 绘制节点的图片及其指向的位置
问题描述
我应该在输入以下代码后直观地解释四个节点是如何出现的:
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 指向一个节点(不变)
- B 变成一个空对象
- C的节点指向A
- D 指向一个节点(不变)
上面两行给出 NullPointer 错误是否正常?我的猜测也有点接近图片吗?感谢您的帮助
解决方案
鉴于每个Object 的Data
and为 null,让我们从您的 main 函数开始逐步了解它。(建议您对这些变量名使用 camelCase https://en.wikipedia.org/wiki/Camel_case)Next
Node
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
推荐阅读
- hyperledger-fabric - Hyperledger 结构:无法在 chainecode_example02 中调用链代码?
- c++ - 在 Windows 上以 Ubuntu 为目标时,如何在 C++ Visual Studio Linux 项目中包含 ncurses.h 库?
- node.js - 如何在循环中向不同的对话流代理发送单个请求?
- python - NoReverseMatch at /accounts/password-reset/ 用于 Django 中的密码重置
- javascript - 如何更正此缓存功能
- c# - 如何让 TextBox 监听 INotifyPropertyChanged
- java - 使用 Unity 2018 的 Google Play 服务排行榜错误 (java.lang.ClassNotFoundException: com.google.games.bridge.TokenFragment)
- excel - 当 DDE 链接提供的值在单元格中更改时如何调用宏
- python - 在同一个图上对 scatter 和 contourf 使用相同的颜色
- java - 如何使用新的导航图将过渡应用于片段?