这段代码有什么问题?它不会运行

时间:2017-08-16 11:28:06

标签: java arrays stack

public class StackSimple{
    private   long  capacity=1000;//maximum size of array
    private int idx_top;
    private Object data[];

    public StackSimple(int capacity)
    {
        idx_top=-1;
        this.capacity=capacity;
        data = new Object[capacity];
    }

    public boolean isEmpty(){
        return(idx_top<0);

    }
    public boolean isFull(){
        return(idx_top>=capacity-1);
    }

    public int size()
    {
        return idx_top+1;
    }

    public boolean push(Object x){
        if (isFull()){
            throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack");
}
        else
        {`enter code here`data[++idx_top]=x;
            return true;
        }
    }

    public Object pop(){
        if(isEmpty())
            throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack.");
        else{
            return data[idx_top--];
        }
}

public Object top(){
    if (isEmpty())
        throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack.");
    else{
        return data[idx_top];
   }
}

public void print()
{`
for (int i=size()-1;i>=0;i--)
        System.out.println(data[i]);
}



}

public class Stack_Exercise {
    public static void main(String[] args) {
        StackSimple s = new  StackSimple(capacity:3);//error shows here
        s.push(x:"books");`enter code here`
        s.push(x:"something");
        s.push(x:"200");
        s.print();
        System.out.println("Size=" +s.size());
    }
}   

为什么这不起作用?  为什么在创建StackSimple对象时会说无效语句?运行它时问题出在主类中。推送元素时会出错。

Error while compiling

3 个答案:

答案 0 :(得分:0)

将参数传递给函数时,只需传递值即可。 在您的情况下,不是StackSimple(capacity:3),而只是StackSimple(3)

答案 1 :(得分:0)

第一个问题,您使用的是哪个版本的Java。

其次,在Java中,您应该作为变量而不是StackSimple传递(容量:3)。将您的主要方法更改为以下,这是我的建议:

    StackSimple s = new  StackSimple(3);
    s.push("books");
    s.push("something");
    s.push("200");
    s.print();
    System.out.println("Size=" +s.size());

答案 2 :(得分:0)

你根本没有推动堆栈中的值,你的pusch函数不能正常工作。

这是正确的程序。

class StackSimple {
    private long capacity = 1000;// maximum size of array
    private int idx_top;
    private Object data[];

    public StackSimple(int capacity) {
        idx_top = -1;
        this.capacity = capacity;
        data = new Object[capacity];
    }

    public boolean isEmpty() {
        return (idx_top < 0);

    }

    public boolean isFull() {
        return (idx_top >= capacity - 1);
    }

    public int size() {
        return idx_top + 1;
    }

    public boolean push(Object x) {
        if (isFull()) {
            throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack");
        } else {
            data[++idx_top] = x;
            return true;
        }
    }

    public Object pop() {
        if (isEmpty())
            throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack.");
        else {
            return data[idx_top--];
        }
    }

    public Object top() {
        if (isEmpty())
            throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack.");
        else {
            return data[idx_top];
        }
    }

    public void print() {
        for (int i = size() - 1; i >= 0; i--)
            System.out.println(data[i]);
    }

}

public class test {
    public static void main(String[] args) {
        StackSimple s = new StackSimple(3);// error shows here
        s.push("books");
        s.push("something");
        s.push("200");
        s.print();
        System.out.println("Size=" + s.size());
    }
}