在令牌上键入语法错误,错放的构造(s)

时间:2012-05-06 10:16:45

标签: java

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).

需要帮助。找不到问题。

1 个答案:

答案 0 :(得分:4)

你不能在方法之外有类似的随机语句。您需要将该语句放在方法中,或者构建一个使用链接列表的类并插入它。