簡體   English   中英

令牌類型錯誤,構造放置錯誤

[英]Type Syntax error on token(s), misplaced construct(s)

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");
}

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

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

需要幫忙。 找不到問題。

您不能在方法外部具有這樣的隨機語句。 您需要將該語句放入方法中,或構建一個使用鏈接列表並進行插入的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM