Java 从链表中的索引中获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18688033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get element from an index in a linked list
提问by Steven
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();" is there something wrong with my get method? e.g of why I want this method: when I call get(0) I want to return the first element in the list
我从“return temp.getElement();”行得到一个空指针异常 我的 get 方法有问题吗?例如为什么我想要这个方法:当我调用 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;
}
}
采纳答案by Daniel L.
When do you initialize your list? Or to be more specific - where is _firstLink
being declared and assigned?
What is the call you're making before getting the null pointer exception?
你什么时候初始化你的列表?或者更具体地说 - 在哪里_firstLink
声明和分配?在获得空指针异常之前,您正在拨打什么电话?
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.
我建议您只需调试此代码并查看您在运行时定义的数据结构。
回答by Arash Saidi
If you are getting a null pointer exception, in this case there are two plausible explanation.
如果您遇到空指针异常,在这种情况下有两种合理的解释。
your list is empty, i.e 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.
your list has only one element. Hence the firstLink.next() would give you a null pointer.
你的列表是空的,即没有 firstLink 开始,你得到一个空指针,因为你试图访问一个尚未初始化的指针。
您的列表只有一个元素。因此 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