简体   繁体   English

从链表中的索引中获取元素

[英]get element from an index in a linked list

I'm writing my own LinkedList class (i know there is one in the API .. etc) I have integers stored in my DLink and getElement() returns the integer stored in the link. 我正在编写自己的LinkedList类(我知道API中有一个类。等等)我在DLink中存储了整数,而getElement()返回了在链接中存储的整数。

I am getting a null pointer exception from the line "return temp.getElement();" 我从“ return temp.getElement();”行中获取空指针异常。 is there something wrong with my get method? 我的get方法有什么问题吗? eg of why I want this method: when I call get(0) I want to return the first element in the list 例如,为什么要这个方法:当我调用get(0)时,我想返回列表中的第一个元素

public int get(int index)
{

       //forces the index to be valid
      assert (index >= 0 && index < size());

      DLink temp = _firstLink; //start at the head of the list

      //iterate to the correct node
      for(int i = 0; i < index; i++)
      {
          temp = temp._next;
      }

      return temp.getElement(); //and return the corresponding element



    }

here is my DLink class if you want to look at it: 如果您想看一下,这是我的DLink类:

//elements in DLink are integers
public class DLink {
    public int _element;

    public DLink _next;
    public DLink _previous;

    public DLink(int e)
    {
        _next = null;
        _previous = null;

        this._element = e;
    }

    public int getElement()
    {
        return _element;
    }

    public void setNext(DLink link)
    {
        _next = link;
    }
    public void setPrev(DLink link)
    {
        _previous = link;
    }

    public DLink getPrev()
    {
        return _previous;
    }

    public DLink getNext()
    {
        return _next;
    }

}

When do you initialize your list? 您何时初始化列表? Or to be more specific - where is _firstLink being declared and assigned? 或者更具体地说_firstLink在哪里声明和分配? What is the call you're making before getting the null pointer exception? 在获取空指针异常之前,您正在进行的呼叫是什么?

Without seeing the context - my guess is that you are not initializing _firstLink correctly. 没有看到上下文-我的猜测是您没有正确初始化_firstLink

I would suggest that you'll simply debug this code and review the data structure you defined in runtime. 我建议您仅调试此代码并查看在运行时中定义的数据结构。

If you are getting a null pointer exception, in this case there are two plausible explanation. 如果遇到空指针异常,在这种情况下,有两个合理的解释。

  1. your list is empty, ie there is no firstLink to begin with, and you get a null pointer because you are trying to access a pointer that is yet to be initialized. 您的列表为空,即没有firstLink开头,并且由于尝试访问尚未初始化的指针而获得空指针。

  2. your list has only one element. 您的列表只有一个元素。 Hence the firstLink.next() would give you a null pointer. 因此,firstLink.next()将为您提供一个空指针。

You should always implement a few checks before entering a while loop that iterates through a list. 在进入遍历列表的while循环之前,应始终执行一些检查。

if(firstLink == null)
    return;

if(firstLink.next() == null)
    return;

or you could have an initial clause before the while-loop 或者你可以在while循环之前有一个初始子句

if(firstLink != null && firstLink.next() != null)
    while(true)
        ...do-something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM