简体   繁体   English

Java 方法未由堆栈解析 object

[英]Java Method isn't resolved by stack object

I was trying to solve a problem named push at bottom of stack.我试图解决一个名为 push at bottom of stack 的问题。 I got the recursion logic but the thing is, I have written a method pushAtbottom but the method is not recognized by the main method and I don't understand why.我得到了递归逻辑,但问题是,我写了一个pushAtbottom方法,但main方法无法识别该方法,我不明白为什么。 The error is 'Can not resolve pushAtbottom'错误是“无法解析 pushAtbottom”

import java.util.Stack;

public class pushatbottom {

    public static void main(String[] args) {

        Stack<Integer> s =new Stack<>();

        s.push(1);
        s.push(2);
        s.push(3);
        s.push(4);
        s.push(5);
        s.pushAtbottom(6,s);
        while(!s.isEmpty())
        {
            System.out.println(s.peek());
            s.pop();
        }
    }

    void pushAtbottom(int data,Stack<Integer> s)
    {
        if(s.isEmpty())
        {
            s.push(data);
        }
        int top=s.pop();
        pushAtbottom(4,s);
        s.push(top);

    }
}

pushAtbottom is a method of your class, not of java.util.Stack . pushAtbottom是您的 class 的一种方法,而不是java.util.Stack的一种方法。 You need to declare it as static (ie, static void ushAtbottom(int data,Stack<Integer> s) ) and then pass the stack to it when calling it form main :您需要将其声明为 static(即static void ushAtbottom(int data,Stack<Integer> s) ),然后在从main调用它时将堆栈传递给它:

pushAtbottom(6, s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM