[英]Insertion List Linked in Java (Debug)
I am trying to build a linked list as is shown below: 我正在尝试建立一个链表,如下所示:
**list-> [C] - [B] - [A] -> NULL**
Here is my main: 这是我的主要内容:
import java.util.*;
class LinkedListF
{
public static void main (String [] args)
{
Node aNode = new Node ("A", null);
Node bNode = new Node ("B", aNode);
Node list = new Node ("C",bNode);
DynamicList dynamicList = new DynamicList();
dynamicList.insertFirst("A");
dynamicList.insertFirst("C");
dynamicList.insertAfter(list, "B");
//the line above is where I am struggling with.
System.out.println(dynamicList.getList().getInfo());
System.out.println(dynamicList.getList().getNext().getInfo());
System.out.println(dynamicList.getList().getNext().getNext().getInfo());
}
}
The problem I am having now, as I mark in the codes, is when the code " dynamicList.insertAfter(list, "B")
" runs, it does not insert "B" after the Node "list" in the dynamicList that I construct. 正如我在代码中标记的那样,我现在遇到的问题是,当代码“
dynamicList.insertAfter(list, "B")
“运行时,它没有在我要查询的dynamicList中的节点“ list”之后插入“ B”构造。 I am very confused about what is happening here. 我对这里发生的事情感到非常困惑。
My output looks like this: 我的输出如下所示:
----jGRASP exec: java LinkedListF
C
A
Exception in thread "main" java.lang.NullPointerException
at LinkedListF.main(LinkedListF.java:27)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
The line 27 is my third System.out.print statement. 第27行是我的第三个System.out.print语句。
Below is the insertAfter method in my DynamicList class: 下面是我的DynamicList类中的insertAfter方法:
public void insertAfter(Node p, String x)
{
if (isEmpty())
{
System.out.println("VOID INSERTION.");
System.exit(1);
}
Node q = new Node (x, p.getNext());
p.setNext(q);
}
Below is the Node class that I use to construct each node: 以下是我用来构造每个节点的Node类:
class Node
{
private String data;
private Node next;
public Node (String data, Node next)
{
this.data = data;
this.next = next;
}
String getInfo()
{
return data;
}
Node getNext()
{
return next;
}
void setInfo(String data)
{
this.data = data;
}
void setNext(Node next)
{
this.next = next;
}
}
The problem is that you are creating several bare Node
objects, which are not part of dynamicList
, so your list is shorter than you think it is, and your print statements are running off the end. 问题在于,您正在创建几个裸
Node
对象,它们不是dynamicList
一部分,因此您的列表比您想像的要短,并且打印语句快要结束了。
Let's look at what your code is actually doing: 让我们看看您的代码实际在做什么:
Node aNode = new Node ("A", null);
Node bNode = new Node ("B", aNode);
Node list = new Node ("C", bNode);
This will create three Node
s which are in a chain 这将创建链中的三个
Node
。
list -> bNode -> aNode -> null
You then create a DynamicList
and add a couple String
s to it: 然后,您创建一个
DynamicList
并向其添加几个String
:
DynamicList dynamicList = new DynamicList();
dynamicList.insertFirst("A");
dynamicList.insertFirst("C");
You haven't shown the code for insertFirst(String)
but we can safely assume it creates a new Node
for each String
you add, which leaves you with this: 您尚未显示
insertFirst(String)
的代码,但是我们可以放心地假设它为您添加的每个String
创建一个新的Node
,这使您可以:
(stand-alone) list -> bNode -> aNode -> null
(dynamicList) "C" -> "A" -> null
Now let's examine what happens when you dynamicList.insertAfter(list, "B");
现在,让我们检查一下
dynamicList.insertAfter(list, "B");
时发生的情况dynamicList.insertAfter(list, "B");
First the method checks to see if the list is empty (which it isn't, since you've added "A" and "C" to it). 首先,该方法检查列表是否为空(因为您已经向其中添加了“ A”和“ C”,所以列表不是空的)。 However, it then creates a new
Node
based on the String
you give it and inserts it after the parameter Node
. 但是,它随后根据您提供的
String
创建一个新的Node
并将其插入到参数 Node
。 Nowhere does it check to see if that Node
actually exists in the DynamicList
. 它无处检查该
Node
确实存在于DynamicList
。
This means that you end up with this: 这意味着您最终会得到以下结果:
(stand-alone) list -> "B" -> bNode -> aNode -> null
(dynamicList) "C" -> "A" -> null
This is what is leading to the NPE when you try to print: 当您尝试打印时,这就是导致NPE的原因:
System.out.println(dynamicList.getList().getInfo());
(dynamicList) "C" -> "A" -> null
^^^ prints "C"
System.out.println(dynamicList.getList().getNext().getInfo());
(dynamicList) "C" -> "A" -> null
^^^ prints "A"
System.out.println(dynamicList.getList().getNext().getNext().getInfo());
(dynamicList) "C" -> "A" -> null
^^^ null has no info to get
Instead of calling the insert method which takes a String
and creates a new Node
, you need to insert the Node
s you've already created (assuming such methods exist): 无需调用采用
String
并创建新Node
的insert方法,而是需要插入已经创建的Node
(假设存在此类方法):
Node aNode = new Node ("A", null);
Node list = new Node ("C", aNode);
// creates list ("C") -> aNode ("A") -> null
DynamicList dynamicList = new DynamicList();
dynamicList.insertFirst(list);
// dynamicList = list ("C") -> aNode ("A") -> null
dynamicList.insertAfter(list, "B");
// dynamicList = list ("C") -> "B" -> aNode ("A") -> null
An even simpler solution would be to never manually create any Node
and just let the DynamicList
do all the work: 甚至更简单的解决方案是永远不要手动创建任何
Node
而只让DynamicList
完成所有工作:
DynamicList dynamicList = new DynamicList();
dynamicList.insertFirst("A");
dynamicList.insertFirst("B");
dynamicList.insertFirst("C");
// dynamicList = "C" -> "B" -> "A" -> null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.