在我的LinkedList-Java中实现Add/Get时遇到问题

在我的LinkedList-Java中实现Add/Get时遇到问题,java,Java,我的代码有点问题,我总是得到空指针。。。我正在尝试使用add-and-get函数创建自己的linkedlist 以下是我现在掌握的代码: public class LinkedList<E> { private class ListNode<E> { public E data; public ListNode<E> next; } private ListNode<E> head; private int size; publ

我的代码有点问题,我总是得到空指针。。。我正在尝试使用add-and-get函数创建自己的linkedlist

以下是我现在掌握的代码:

public class LinkedList<E> {
private class ListNode<E> {
    public E data;
    public ListNode<E> next;
}

private ListNode<E> head;
private int size;

public LinkedList() {       
    size = 0;
}

public int size() {
    return size;
}
public void add(E e) {
    if(size == 0){ // first node in the list

        ListNode<E> firstNode = new ListNode<E>();
        firstNode.data = e;
        firstNode.next = null;

        size++; //increment size counter 
        // set this node equal to first node in the linked list
        this.head = firstNode;

    }
    else{ // not the first node
        ListNode<E> temp = head;
        while(temp != null){ // find the last node
            temp = temp.next;
        }

        // create new node to be added to linked list
        ListNode<E> newNode = new ListNode<E>();
        newNode.data = e;
        newNode.next = null;

        // set the previous last node to the new, created node
        temp = newNode;

        size++; //increment size of the LinkedList
    }
}
public E get(int index) {
    if( index > size){ // account for if user inputs something that is reachable within LinkedList
        return null;
    }
    ListNode<E> temp = head;
    if(index == 0){
        return head.data;
    }
    else{
        for(int i = 0; i < index; i++){
            temp = temp.next;
        }
        return temp.data;
    }

    }
公共类链接列表{
私有类ListNode{
公共电子数据;
公共列表节点下一步;
}
私有节点头;
私有整数大小;
公共链接列表(){
尺寸=0;
}
公共整数大小(){
返回大小;
}
公共空间添加(E){
如果(size==0){//列表中的第一个节点
ListNode firstNode=新ListNode();
firstNode.data=e;
firstNode.next=null;
size++;//递增大小计数器
//将此节点设置为链接列表中的第一个节点
this.head=firstNode;
}
else{//不是第一个节点
ListNode温度=头;
while(temp!=null){//查找最后一个节点
温度=下一个温度;
}
//创建要添加到链接列表的新节点
ListNode newNode=新ListNode();
newNode.data=e;
newNode.next=null;
//将上一个节点设置为新创建的节点
temp=newNode;
size++;//增加LinkedList的大小
}
}
公共E-get(int索引){
if(index>size){//说明用户是否输入了可在LinkedList中访问的内容
返回null;
}
ListNode温度=头;
如果(索引==0){
返回头数据;
}
否则{
对于(int i=0;i
现在,在我的main中,我添加了一个名为num的整数链接列表,并在其中添加了数字0-9。现在,当我使用函数System.out.println(num.get(1)),我得到了nullExceptionError,它突出显示了get函数的返回temp.data


我认为这与我的添加有关,或者与没有正确指向我的节点有关……

您的问题是您正在正确更新列表中节点中的
下一个
引用

添加新元素时,您的目标是将linkedList中最后一个
ListNode
next
字段设置为与具有特定数据字段的新
ListNode
对象相等

但是,您尝试使用以下行添加新节点:

temp = newNode;
由于temp是一个局部变量,所以上面的行只在局部更新
temp
的值

您应该更新链表中最后一个对象的
next
字段

您的代码应该如下所示

  else{ // not the first node
    ListNode<E> temp = head;
    while(temp.next != null){ // find the last node
        temp = temp.next;
    }

    // create new node to be added to linked list
    ListNode<E> newNode = new ListNode<E>();
    newNode.data = e;
    newNode.next = null;

    // set the previous last node to the new, created node
    temp.next = newNode;

    size++; //increment size of the LinkedList
}
else{//不是第一个节点
ListNode温度=头;
while(temp.next!=null){//查找最后一个节点
温度=下一个温度;
}
//创建要添加到链接列表的新节点
ListNode newNode=新ListNode();
newNode.data=e;
newNode.next=null;
//将上一个节点设置为新创建的节点
temp.next=newNode;
size++;//增加LinkedList的大小
}

您的问题是您正在正确更新列表中节点内的
下一个
引用

添加新元素时,您的目标是将linkedList中最后一个
ListNode
next
字段设置为与具有特定数据字段的新
ListNode
对象相等

但是,您尝试使用以下行添加新节点:

temp = newNode;
由于temp是一个局部变量,所以上面的行只在局部更新
temp
的值

您应该更新链表中最后一个对象的
next
字段

您的代码应该如下所示

  else{ // not the first node
    ListNode<E> temp = head;
    while(temp.next != null){ // find the last node
        temp = temp.next;
    }

    // create new node to be added to linked list
    ListNode<E> newNode = new ListNode<E>();
    newNode.data = e;
    newNode.next = null;

    // set the previous last node to the new, created node
    temp.next = newNode;

    size++; //increment size of the LinkedList
}
else{//不是第一个节点
ListNode温度=头;
while(temp.next!=null){//查找最后一个节点
温度=下一个温度;
}
//创建要添加到链接列表的新节点
ListNode newNode=新ListNode();
newNode.data=e;
newNode.next=null;
//将上一个节点设置为新创建的节点
temp.next=newNode;
size++;//增加LinkedList的大小
}

是的,这一切都与您的添加有关

仔细查看
add()
方法中的
else
块。您创建了一个新节点,但从未将其附加到列表的末尾:您不会使任何现有节点的
下一个
字段指向新节点

else{ // not the first node
    ListNode<E> temp = head;
    while(temp != null){ // you actually go one past the last node
        temp = temp.next;
    }
    // at this point temp points to null, not to the last node

    ListNode<E> newNode = new ListNode<E>();
    newNode.data = e;
    newNode.next = null;

    temp = newNode;
    // temp used to point to null, now it points to your new node,
    // but it doesn't make newNode a part of your list

    size++;
}

是的,这一切都与你的添加有关

仔细查看
add()
方法中的
else
块。您创建了一个新节点,但从未将其附加到列表的末尾:您不会使任何现有节点的
下一个
字段指向新节点

else{ // not the first node
    ListNode<E> temp = head;
    while(temp != null){ // you actually go one past the last node
        temp = temp.next;
    }
    // at this point temp points to null, not to the last node

    ListNode<E> newNode = new ListNode<E>();
    newNode.data = e;
    newNode.next = null;

    temp = newNode;
    // temp used to point to null, now it points to your new node,
    // but it doesn't make newNode a part of your list

    size++;
}