I was trying to solve a problem named 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. The error is 'Can not resolve 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
. 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
:
pushAtbottom(6, s);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.