链表错误(java)



我创建了自己的链表,但是当我试图运行它时出现错误:

Exception in thread "main" java.lang.NullPointerException
at List.add(List.java:8)  //if(t.val ==null)
at main.main(main.java:38) //linput.add(inputLine.split(" ")[i]);
下面是我的List类:
class List{
    String val;
    List next=null; 
    private List t; 
    public void add(String word){   
        if(t.val ==null)
            t.val=word;
        else while(!t.next.val.equals(null))
        {
            t=t.next;
            if(t.next.val.equals(null))
            {
                t.next.val=word;
                break;
            }
        }
    }
    public int get(String word)
    {   
        int i=0;
        if(t.val.equals(word))
            i=0;
        else while(!t.next.val.equals(word))
        {
            t=t.next;
            i++;
            if(t.next.val.equals(word))
            {
                i++;
            }
        }
        return i;
    }
    public String indexOf(int i)
    {   
        int counter=0;
        while(counter<i)
        {
            t=t.next;
            counter++;
        }
        return t.val;
    }
}

下面是我的主要函数:

static public void main(String[] args)  
{   
    List linput = new List();
    String inputLine = "Hey look at me.";
    for(int i = 0 ; i < inputLine.split(" ").length ; i++)
    {
        linput.add(inputLine.split(" ")[i]);
    }
    System.out.println(linput.indexOf(0)+" "+linput.indexOf(1)+" "+linput.indexOf(2));
}

我初始化了t,但下次会出现这样的错误:

private List t =new List();
Exception in thread "main" java.lang.StackOverflowError
    at List.<init>(List.java:5)
    at List.<init>(List.java:5)
    at List.<init>(List.java:5)

抱歉。我不能给我的完整代码,因为我的代码的其余部分工作良好(从txt读取等....)。

错误似乎与变量't'(即私有List t)有关。你初始化这个变量了吗?if (t.val == null)似乎是在模仿这个,因为此时t是null(未初始化)您应该为这个变量分配对象(使用new)。

你能分享List构造函数的完整代码吗?

假设您想要实现一个简单的转发列表,而不是使用Java LinkedList class,您需要:

  • 将列表的实现更改为列表
  • 中的参考节点
  • 处理单词列表中链接节点的遍历

下面是一个例子:

<<p> WordList类/strong>
package com.example.words;
class WordList {
    private WordNode head = null;
    private int listSize = 0;
    public void add(String word) {
        // TODO add check for duplicate word
        if (head == null) {
            head = new WordNode();
            head.setValue(word);
            listSize++;
        } else {
            WordNode current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            WordNode newNode = new WordNode();
            newNode.setValue(word);
            current.setNext(newNode);
            listSize++;
        }
    }
    public int getWordIndex(String word) {
        WordNode current = head;
        int index = 0;
        boolean found = false;
        while (!found && current != null) {
            found = current.getValue().equalsIgnoreCase(word);
            if (!found) {
                index++;
                current = current.getNext();
            }
        }
        if (found) {
            return index;
        } else {
            return -1;
        }
    }
    public String indexOf(int i) {
        int index = 0;
        WordNode current = head;
        if (i <= listSize) {
            while (index < i) {
                current = current.getNext();
                index++;
            }
            return current.getValue();
        } else {
            return null;
        }
    }
    public int size() {
        return listSize;
    }
}
<<p> WordNode类/strong>
package com.example.words;
public class WordNode {
    private String value;
    private WordNode next = null;
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public WordNode getNext() {
        return next;
    }
    public void setNext(WordNode link) {
        next = link;
    }
}

测试驱动程序

package com.example.words;
public class Test {
    public static void main(String[] args) {
        //TODO handle punctuation
        WordList myList = new WordList();
        String inputLine = "Hey look at me.";
        String[] pieces = inputLine.split(" ");

        for (int i=0; i < pieces.length; i++) {
            myList.add(pieces[i]);
        }
        for (int i=0; i < pieces.length; i++) {
            String value = myList.indexOf(i);
            if (value.equalsIgnoreCase(pieces[i])) {
                System.out.println("Following node is wrong:");
            }
            System.out.println ("node " + i + ". = " + value);
        }
    }
}

您尝试将t创建为自己类的成员变量,如下所示:

class List {
    [...]
    private List t = new List();
    [...]
}

这将不起作用,因为List的构造函数将被无限调用。

尝试延迟实例化t。用getter替换t的所有访问:

private List getT() {
    if (this.t == null) {
        this.t = new List();
    }
    return t;
}

相关内容

  • 没有找到相关文章

最新更新