java 标记上的类型语法错误,错位的构造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10469763/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:11:17  来源:igfitidea点击:

Type Syntax error on token(s), misplaced construct(s)

java

提问by shutdowncore

package list;

public class LinkedList implements List {

    //Datenfeld
    private Element item;

    //Zeigerfeld
    private LinkedList next;

    //Konstruktor, erzeugt leere Liste
    public LinkedList() {
        item = null;
        next = null;
    }

    //Selektoren
    public Object getItem() {
        return item;
    }

    public LinkedList next() {
        return next;
    }

    //ist die Liste leer?
    public boolean isEmpty() {
        return next == null;
    }

    public Object firstElement() {
        if (isEmpty()) 
            return null;
        else
            return next.item;
    }

    public int length() {
        if (isEmpty())
            return 0;
        else
            return 1 + next.length();
    }

    //fügt x am Kopf ein
    public LinkedList insert(Element x) {
        LinkedList l = new LinkedList();
        l.item = x;
        l.next = next;
        next = l;
        return this;
    }

    //h?ngt das x ans Ende der Liste und liefert Teilliste
    public LinkedList append (Element x) {
        if (isEmpty())
            return insert(x);
        else
            return next.append(x);
    }

    //liefert Null, falls x nicht in Liste
    //sonst Teilliste
    private LinkedList find(Element x) {
        if (isEmpty()) 
            return null;
        else
            if (firstElement().equals(x))
                return this;
            else
                return next.find(x);
    }

    //entfertn erstes x der Liste
    public LinkedList delete(Element x) {
        LinkedList l = find(x);
        if (l != null)
            return l.next = l.next.next;
        else
            return this;
    }

    //entfernt das erste Element der Liste
    public LinkedList delete() {
        if (!isEmpty()) 
            next = next.next;
        return this;    
    }

    public boolean isInList(Element x) {
        return(find(x) != null);
    }

    public String toString() {
        return (next == null ? " |--" : " --> " + next.item + next);
    }

    static void println(Element x) {
        System.out.println(x.toString());
    }

    public LinkedList add(Element x, int n) {
        return null;
    }

    public LinkedList remove(int n) {
        return null;
    }

    public Element get(int n) {
        return null;
    }

    public int firstIndexOf(Element x) {
        return 1;
    }

    public int lastIndexOf(Element x) {
        return 1;
    }


    LinkedList l1 = new LinkedList();

    l1.insert("AA");
}

In the last line (l1.insert("AA");I get the error

在最后一行(l1.insert("AA");我收到错误

Type Syntax error on token(s), misplaced construct(s).

Need help. Can't find the problem.

需要帮忙。找不到问题。

回答by Mat

You can't have random statements like that outside of methods. You need to put that statement in a method, or build a class that uses your linked lists and does that insert.

你不能在方法之外有这样的随机语句。您需要将该语句放在一个方法中,或者构建一个使用您的链表并执行插入操作的类。