gpt4 book ai didi

java - 将节点添加到指定索引Java的双向链表时出现NullPointerException

转载 作者:行者123 更新时间:2023-11-30 11:54:28 25 4
gpt4 key购买 nike

我根本不知道我哪里出错了,我已经检查并重新检查了我的 add(int index, T obj) 方法无数次,仍然出现同样的错误。这是我的代码,任何指针将不胜感激;这个问题至少现在已经阻碍了我的项目一天左右。

package edu.neumont.csc250;


class LinkedList<T> implements List<T>{

Node<T> head;
Node<T> tail;
int listCount;

public LinkedList(){
head = null;
listCount = 0;
}

@Override
public T get(int index) throws IllegalArgumentException {
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{
Node<T> current = head;
for(int i = 0; i < index; i++)
{
current = current.next;
}
if(current.content != null){
return current.content;
}
else{
System.out.println("Null value.");
return null;
}
}
}

@Override
public void add(T obj) {
if(head == null){
head = new Node<T>(obj);
head.next = null;
tail = head;
listCount++;
}
else{
if(head.next == null){
head.next = new Node<T>(obj);
//head.next.next = null;
tail = head.next;
tail.prev = head;
listCount++;
}
else{
tail.next = new Node<T>(obj);
tail.next.prev = tail;
tail = tail.next;
tail.next = null;
listCount++;
}
}
}

@Override
public void add(int index, T obj) throws IllegalArgumentException {
// TODO Auto-generated method stub
Node<T> temp = new Node(obj);
Node<T> current = head;

for(int i = 0; i<=index; i++){
current = current.next;
}
temp.prev = current.prev;
current.prev = temp;
current.prev.next = current;

if(index == 0){
head = current.prev;
}
else if(index == size()+1){
tail = current.next;
}

listCount++;
}

@Override
public void replace(int index, T obj) throws IllegalArgumentException {
// TODO Auto-generated method stub
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{
//get(index)
}

}

@Override
public T remove() {
head = head.next;
listCount--;

return null;
}

@Override
public T remove(int index) throws IllegalArgumentException {
// TODO Auto-generated method stub
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{

listCount--;
}

return null;
}

@Override
public int size() {
return listCount;
}

public static void main(String[] args){
LinkedList<String> list = new LinkedList<String>();
list.add("Red");
list.add("Orange");
list.add("Yellow");
list.add("Green");
list.add("Blue");
list.add("Purple");

for(int a = 0; a < list.size(); a++){
System.out.println(list.get(a));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");

list.remove();

for(int b = 0; b < list.size(); b++){
System.out.println(list.get(b));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
//System.out.println(list.get(5));
System.out.println("There are " + list.size() + " colors in the list.");

list.add(0, "Red");
System.out.println(list.size());

for(int c = 0; c < list.size(); c++){
System.out.println(list.get(c));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
}

class Node<T>{
T content;
Node<T> next;
Node<T> prev;

public Node(T content){
this.content = content;
}

public T getContents(){
return content;
}

public void printNode() {
System.out.print("{" + content + "} ");
}
}
}

如果有任何帮助,这是我的控制台读数:

Red Orange Yellow Green Blue Purple There are 6 colors in the list. Orange Yellow Green Blue Purple There are 5 colors in the list. 6 Red Yellow Green Blue Purple Exception in thread "main" java.lang.NullPointerException at edu.neumont.csc250.LinkedList.get(LinkedList.java:26) at edu.neumont.csc250.LinkedList.main(LinkedList.java:161)

编辑:根据要求隔离主要方法:

public static void main(String[] args){
LinkedList<String> list = new LinkedList<String>();
list.add("Red");
list.add("Orange");
list.add("Yellow");
list.add("Green");
list.add("Blue");
list.add("Purple");

for(int a = 0; a < list.size(); a++){
System.out.println(list.get(a));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");

list.remove();

for(int b = 0; b < list.size(); b++){
System.out.println(list.get(b));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
//System.out.println(list.get(5));
System.out.println("There are " + list.size() + " colors in the list.");

list.add(0, "Red");
System.out.println(list.size());

for(int c = 0; c < list.size(); c++){
System.out.println(list.get(c));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
}

最佳答案

当 insert(add()) 索引为 0 时,新元素将插入到第一个元素之后。
但是head引用指向的是新插入的元素,而不是操作完成后实际的head元素。
因此,当您迭代列表时,将发生 NPE。
事实上,你可以从程序的输出中得到这个。请注意,第三次迭代从“红色”开始,“橙色”消失。


而 remove() 的实现也不好,因为它会导致“内存泄漏”。它只是将“头部”向前移动,而不会使元素为空。

关于java - 将节点添加到指定索引Java的双向链表时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5698235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com