[英]multiple variables stored in single node java linked list
I was curious as to learn how you would add multiple ints to a Node in a LinkedList in java (single circular). 我很好奇如何学习如何在java(单循环)的LinkedList中向Node添加多个int。 I had found a thread on SO and was reading on it but wasn't sure exactly how it worked. 我在SO上发现了一个帖子,正在阅读它,但不确定它是如何工作的。 Thought I would revive the question to see if I can get an answer. 以为我会重新回答这个问题,看看能不能得到答案。
This is my Node class 这是我的Node类
public class LinkedList{
private class Node{
private int pid;
private int time;
private Node next;
public Node(int pid, int time){
this.pid=pid;
this.time=time;
}
}
int size;
Node head;
This is my add which I'm just trying before I do any remove or anything like that. 这是我的添加,我只是在做任何删除或类似之前尝试。
public void add(int pid, int time) {
Node curr=head;
Node newNode=new Node(pid, time);
if(head==null){
head=newNode;
newNode.next=head;
}//end if
else{
while(curr.next!=head){
curr = curr.next;
}//end while
curr.next=newNode;
newNode.next=head;
}//end else
size++;
}//end add
}
This is what I have so far but when I try to input the two ints I get a null pointer exception at the private int time
Am I doing something wrong? 这是我到目前为止,但当我尝试输入两个整数时,我在private int time
得到一个空指针异常我做错了什么? I'm reading in a file and then storing the two ints in a single node and then doing the same until the file is completely read through. 我正在读取一个文件,然后将两个整数存储在一个节点中,然后执行相同操作直到完全读取该文件。 I have the file reading in just fine and I have the two ints stored as ints from the file but I can't seem to get it to store the ints in the Node quite yet 我有文件读取就好了,我有两个整数存储为文件的整数但我似乎无法让它在节点中存储整数
How have you initialized head
? 你是如何初始化head
? Did you do Node head = new Node()
? 你做Node head = new Node()
吗?
If you make a custom constructor, Java does not add the default constructor anymore. 如果您创建自定义构造函数,Java将不再添加默认构造函数。 You have to define that again. 你必须再次定义它。
You can instead do Node head = null;
你可以改为做Node head = null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.