所以我的代码中有一些错误,但我不确定他们告诉我要更改什么。这是我的第一个链表代码。如果有人可以帮助我,我将不胜感激。
这是我的链接列表
public class MyLinkedList<E>
{
private Node<E> head = null;
public void add(E element)
{
if(size() == 0)
{
head = new Node<E>(element);
return;
}
Node<E> cursor = head;
while (cursor.next != null)
{
cursor = cursor.next;
}
cursor.next = new Node<E>(element);
}
public void add(int index, E element)
{
Node<E> cursor = head;
E temp, before;
for(int x = 0; x < index; x++)
{
cursor = cursor.next;
}
before = cursor.content;
cursor.content = element;
while(cursor.next != null)
{
cursor = cursor.next;
temp = cursor.content;
cursor.content = before;
before = temp;
}
add(before);
}
public boolean remove(E element)
{
Node<E> cursor = head;
if (head.content == element)
{
head = cursor.next;
return true;
}
while(cursor.next != null)
{
if (cursor.next.content == element)
{
cursor.next = cursor.next.next;
}
else
{
cursor = cursor.next;
}
}
if (cursor.next == null)
{
return false;
}
return true;
}
public E remove(int index)
{
E result = null;
if (index < 0 || index >= size())
{
return null;
}
Node<E> cursor = head;
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
result = cursor.content;
cursor = head;
for (int x = 0; x < index - 1; x++)
{
cursor = cursor.next;
}
if(index != 0)
{
cursor.next = cursor.next.next;
}
else
{
head = cursor.next;
}
return result;
}
public E set(int index, E element)
{
Node<E> cursor = head;
E temp;
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
temp = cursor.content;
cursor.content = element;
return temp;
}
public boolean contains(E element)
{
Node<E> cursor = head;
while(cursor != null)
{
if(cursor.content == element)
{
return true;
}
cursor = cursor.next;
}
return false;
}
public E get(int index)
{
Node<E> cursor = head;
if (index < 0 || index >= size())
{
return null;
}
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
return cursor.content;
}
public int indexOf(E element)
{
Node<E> cursor = head;
int index = 0;
while (cursor != null)
{
if(cursor.content == element)
{
return index;
}
index++;
cursor = cursor.next;
}
return -1;
}
public boolean isEmpty()
{
if (size() == 0)
{
return true;
}
return false;
}
public int size()
{
Node<E> cursor = head;
int count = 0;
while (cursor != null)
{
count++;
cursor = cursor.next;
}
return count;
}
public void dumpList()
{
Node<E> cursor = head;
while (cursor != null)
{
System.out.println(cursor.content);
cursor = cursor.next;
}
}
}
这是我的节点代码
public class Node<E>
{
public E content;
public Node<E> next;
public Node(E content)
{
this.content = content;
}
public Node(E content, Node<E> next)
{
this(content);
this.next = next;
}
public String toString()
{
return content.toString();
}
}
这是我们正在测试的代码
public class Demo4
{
public static void main(String[] args)
{
MyLinkedList<String> t = new MyLinkedList<String>();
t.add("Santa Maria");
t.add("Los Angeles");
t.add("Ventura");
t.add("Thousand Oaks");
t.add(0, "Orcutt");
t.add(5, "Pismo");
t.add(3, "San Luis Obispo");
t.set(1, "London");
t.set(0, "San Diego");
t.set(6, "Tokyo");
t.add("Westlake");
t.remove("Santa Maria");
System.out.println("was Tokyo found? " + t.remove("Tokyo"));
t.remove("Westlake");
System.out.println("was Dubai found? " + t.remove("Dubai"));
t.remove("Pismo");
System.out.println("Remove index 5. It contained: " + t.remove(5));
System.out.println("Remove index 0. It contained: " + t.remove(0));
System.out.println("Remove index 2. It contained: " + t.remove(2));
System.out.println("Here's what's left over");
for (int x = 0; x < t.size(); x++)
{
System.out.println(t.get(x));
}
System.out.println("--------");
System.out.println("Cool! I didn't crash!");
}
}
my error in eclipse is the following
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
最佳答案
看,Eclipse 正在告诉您人类可以告诉您的一切:)
这是你的错误:
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
当调用 add() 方法时,Eclipse 在文件 MyLinkedList.java 的第 35 行中显示“存在空指针异常”。这实际上是在第 12 行调用了 MyLinkedListDemo.java 的 main() 。
现在在该行上放置一个调试点,您将看到什么是 null 以及为什么它是 null。 当您尝试在 null 上调用某些内容时,您会得到一个 NPE
关于Java链表-NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978307/