Java 在链表的末尾添加一个节点,该节点有一个后指针和一个开始指针
我试图将一个节点添加到链接列表的末尾,当我转到else语句并尝试在我添加的新节点旁边设置problem.rear。由于某种原因,当我试图设置指向新节点的rear.next指针时,会弹出异常 澄清一下, BigIntegerList是节点的链表,也是通过将单个整数链接在一起表示大整数的链表。大整数列表定义了“开始”和“后”节点,大整数节点定义了数据的“x”和指向列表中下一个节点的指针的“下一个” 而且 problem.n表示要读入的大整数的数量,该方法从文本文件中读取,第一个数字read表示要读取的大整数数量,后面是实际的大整数 欢迎任何想法,因为我被卡住了Java 在链表的末尾添加一个节点,该节点有一个后指针和一个开始指针,java,nodes,singly-linked-list,Java,Nodes,Singly Linked List,我试图将一个节点添加到链接列表的末尾,当我转到else语句并尝试在我添加的新节点旁边设置problem.rear。由于某种原因,当我试图设置指向新节点的rear.next指针时,会弹出异常 澄清一下, BigIntegerList是节点的链表,也是通过将单个整数链接在一起表示大整数的链表。大整数列表定义了“开始”和“后”节点,大整数节点定义了数据的“x”和指向列表中下一个节点的指针的“下一个” 而且 problem.n表示要读入的大整数的数量,该方法从文本文件中读取,第一个数字read表示要读取
BigIntegerList problem;
LinkedList x;
BigIntegerNode curr;
problem = new BigIntegerList();
//get value of first line stating #of bigInts to read in
problem.n = LinkedList.readInteger(in);
//read big ints from text file based on first number passed, n
for(int i=0; i<problem.n;i++)
{
x = new LinkedList();
x.readBigInteger(in);
//case that the list is empty
if(problem.n<1)
{
problem.start = new BigIntegerNode(x,null);
problem.rear = problem.start;
//list is not empty, add nodes to rear
}else
{
curr = new BigIntegerNode(x,null);
problem.rear.next = curr; -----> this is where i get a nullpointer....
problem.rear = curr;
}
}
return problem;
大整数问题;
LinkedList x;
大整数节点电流;
问题=新的BigIntegerList();
//获取第一行的值,该行表示要读入的大整数的#
problem.n=LinkedList.readInteger(in);
//根据传递的第一个数字从文本文件中读取大整数,n
对于@dave建议的(inti=0;i),将if(problem.n<1)
更改为
If (i < 1) {
If(i<1){
problem.n
是循环将执行的总迭代次数,即常数。i
是循环的计数器。它将在第一次迭代中设置为0,然后设置为1、2、3,…(problem.n)-1
因为您希望if语句在第一次迭代中求值为true,所以让它看i而不是problem.n看您的代码,似乎是if(problem.n<1)
的计算结果总是false
。因此,您的else
块总是带问题执行。rear
仍然null
。您是否尝试过使用调试器进行调试?您介意再解释一下吗?为了可能阅读此答案的新用户。