未解决的编译错误

时间:2013-03-06 17:28:46

标签: java compilation trie

这是我在压缩后缀trie中插入节点的代码:

public class tree {

    char a[] = new char[10];
    a[0]='b';
    a[1]='a';
    a[2]='n';
    a[3]='a';
    a[4]='n';
    a[5]='a';
    a[6]=' ';

    protected node root;

    public tree() {
        this.root = new node();
    }

    public void inorder(node n) {

        if (n.getChildren().next != null) {
            inorder(n.getChildren().next.getChild());
        }

        System.out.println(n.getStart() + n.getEnd());

        if (n.getChildren().next.next != null) {
            inorder(n.getChildren().next.next.getChild());
        }

    }

    public void insert(node n, node r) {

        while (n.getStart() != 6) {

            if (r.getChildren().next == null) {

                //when the tree is empty :

                n.setParent(r);
                n.getChildren().setNext(null);

                link newn = new link();
                newn.setNext(null);
                newn.setChild(n);

                r.getChildren().next = newn;

                node newnode = n;
                newnode.setStart(n.getStart() + 1);

                insert(newnode, r);

            }

            else {

                // count is the node where we begin checking for same letters :

                node count = r.getChildren().next.getChild();

                // l is the linked list containing children of the node : 

                link l = r.getChildren().next;

                while ((a[count.getStart()] != a[n.getStart()])
                        || (l.next != null)) {
                    l = l.next;
                    count = l.getChild();
                }

                // create a new link node corresponding to the node to be inserted
                // only for the case when we reach the end of the list :

                link h = new link();
                h.setNext(null);
                h.setChild(n);

                // we have reached the end of the linked list :

                if ((a[count.getStart()] != a[n.getStart()])
                        && (l.next == null)) {

                    if (n.getStart() != n.getEnd()) {

                        l.setNext(h);
                        h.setNext(null);
                        h.setChild(n);

                        n.setParent(r);
                        n.getChildren().setNext(null);

                        node newnode = n;
                        newnode.setStart(n.getStart() + 1);

                        insert(newnode, r);

                    }

                    else {
                        l.setNext(h);
                        h.setNext(null);
                        h.setChild(n);

                        n.setParent(r);
                        n.getChildren().setNext(null);

                        node newnode = new node();
                        newnode.setStart(count.getStart() + 1);
                        newnode.setEnd(n.getEnd());

                    }
                }

                // if  we have found an element whose characters 
                // match that of the node:

                else {

                    link kids = count.getChildren();

                    int x = count.getStart();
                    int y = n.getStart();

                    int p = count.getEnd();
                    int q = n.getEnd();

                    int t1 = count.getStart();
                    int t2 = n.getStart();

                    int length = p - x + 1;

                    int same = 1;

                    while (a[x + 1] == a[y + 1]) {

                        x++;
                        y++;
                        same++;

                    }

                    int g = length - same;

                    //modifying the node r:

                    count.setStart(t1);
                    count.setEnd(x);

                    // creating the 2 new nodes to be inserted below count if 
                    // count initially doesnt have any children :

                    node kid1 = new node();
                    kid1.setStart(x + 1);
                    kid1.setEnd(p);
                    kid1.getChildren().setNext(null);

                    node kid2 = new node();
                    kid2.setStart(y + 1);
                    kid2.setEnd(q);

                    // creating 2 new link nodes to be inserted in 
                    // the children list of count :

                    link k1 = new link();
                    link k2 = new link();

                    k1.setChild(kid1);
                    k2.setChild(kid2);

                    k1.setNext(k2);
                    k2.setNext(null);

                    //changing relationships :

                    kid1.setChildren(kids);
                    kid1.setParent(count);

                    count.getChildren().next.setNext(k1);

                    while (kids.next != null) {
                        kids.next.getChild().setParent(kid1);
                        kids = kids.next;
                    }

                    insert(kid2, count);
                }
            }
        }
    }

    public static void main(String[] args) {
        tree t = new tree();

        node banana = new node();
        banana.setStart(0);
        banana.setEnd(7);
        banana.getChildren().setNext(null);

        t.insert(banana, t.root);

        //inorder(tree.root);
    }
}

当我在Eclipse中运行它时,它说它有一些未解决的编译问题。 你能帮帮我解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:2)

您不能在类块中包含非声明性语句。

a[0]='b';
a[1]='a';
...

所有这些数组赋值都属于方法,构造函数或static初始化程序块。或者,如果调整长度或数组,可以选择使用:

char a[] = "banana".toCharArray();