多个嵌套类Java

时间:2017-05-20 18:30:12

标签: java class

嗨所以我有一个类,里面有多层嵌套类,我想知道为什么它不起作用。出于某种原因,我无法创建一个新的StackNode,我想知道为什么会这样。

public class MultiStack {
    Stack1[] arr;
    int lim,numplate,index;

    MultiStack(int limit, int total) {
        lim = limit;
        numplate = total;
        int ssize =0;
        if(total%lim==0) {
            ssize = total/lim;
        } else {
            ssize = (total/lim) +1;
        }
        arr = new Stack1[ssize];
    }

    class Stack1 {
        StackNode top;
        int size;

        class StackNode{
            StackNode next;
            int value;
        }
    }
    public void push(int value){
        if(arr[index].size < lim) {
            push1(index);
        }
    }
    public void push1(int index) {
        arr[index].size++;
        StackNode cur = new StackNode(); //here is my issue
        cur.next = arr[index].top;
        arr[index].top = cur;
    }
}

1 个答案:

答案 0 :(得分:1)

课程Stack1和课程StackNode都不是静态

因此,您需要一个周围类的实例来实例化它。

当您尝试实例化类StackNode时,您处于MultiStack的对象中:

public void push1(int index) {
    arr[index].size++;
    Stack1.StackNode cur = new Stack1().new StackNode(); // here is my issue
    cur.next = arr[index].top;
    arr[index].top = cur;
}