Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初始化可以找到最小数量的堆栈。JAVA_Java_Stack - Fatal编程技术网

初始化可以找到最小数量的堆栈。JAVA

初始化可以找到最小数量的堆栈。JAVA,java,stack,Java,Stack,我使用这样的代码,但在运行时有错误。。。系统返回java.util.EmptyStackException..有人能帮我吗 public class Solution { private Stack<Integer> val = new Stack<>(); private Stack<Integer> stackMin = new Stack<>(); Integer temp = null; public v

我使用这样的代码,但在运行时有错误。。。系统返回java.util.EmptyStackException..有人能帮我吗

public class Solution {

    private Stack<Integer> val = new Stack<>();
    private Stack<Integer> stackMin = new Stack<>();
    Integer temp = null;

    public void push(int node) {
        this.val.push(node);
        if(this.stackMin == null){
            this.stackMin.push(node);
        }else if(node<=this.min()){
            this.stackMin.push(node);
        }
    }

    public void pop() {
        if (this.val==null) {
            throw new RuntimeException("Stack is empty.");
        }
        int value = this.val.pop();
        if(value == this.min()){
            this.stackMin.pop();
        }
    }

    public int top() {
        if(this.val!=null){
            return this.val.peek();
        }else{
            throw new RuntimeException("Stack is empty");
        }
    }

    public int min() {
        if(this.stackMin!=null){
            return this.stackMin.peek();
        }
       throw new RuntimeException("Stack is empty");
    }
}
公共类解决方案{
私有堆栈val=新堆栈();
私有堆栈stackMin=新堆栈();
整数temp=null;
公共无效推送(int节点){
this.val.push(节点);
if(this.stackMin==null){
此.stackMin.push(节点);

}else if(node我认为应该使用
stack.empty()
而不是测试空值。我更改了一些代码,异常不会像这样发生

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Main m1 = new Main();
        m1.push(2);
        m1.push(1);
        m1.push(3);
        System.out.println(m1.min());
    }
    private Stack<Integer> val = new Stack<>();
    private Stack<Integer> stackMin = new Stack<>();
    Integer temp = null;

    public void push(int node) {
        this.val.push(node);
        if(this.stackMin.empty()){
            this.stackMin.push(node);
        }else if(node<=this.min()){
            this.stackMin.push(node);
        }
    }

    public void pop() {
        if (!this.val.empty()) {
            throw new RuntimeException("Stack is empty.");
        }
        int value = this.val.pop();
        if(value == this.min()){
            this.stackMin.pop();
        }
    }

    public int top() {
        if(!this.val.empty()){
            return this.val.peek();
        }else{
            throw new RuntimeException("Stack is empty");
        }
    }

    public int min() {
        if(!this.stackMin.empty()){
            return this.stackMin.peek();
        }
        throw new RuntimeException("Stack is empty");
    }
}

在什么调用中会出现此异常?发现错误…我应该使用isEmpty()检查堆栈是否为空。。。
/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java Main
1

Process finished with exit code 0