java 在java中反转堆栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44307564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reversing a stack in java
提问by Danah
public static void reverse(Stack arr){
Queue<Integer> arrCopy=new LinkedList<Integer>();
while(!arr.empty()){
arrCopy.add((int)arr.pop());
};
System.out.print("{ ");
while(!arrCopy.isEmpty()){
System.out.print((int)arrCopy.remove() + ", ");
}
System.out.print("}");
}
so, i have a stack with 10 integers and i'd like to print it in reverse. i wrote a new method that creates a queue, and every time it removes and returns an integer from the stack using pop, it will add it onto the queue. the problem is that while(!arrCopy.isEmpty())
does not seem to be executing and the queue is empty. is there a casting problem here? or is there something wrong with the way i've added elements to the queue?
所以,我有一个包含 10 个整数的堆栈,我想反向打印它。我编写了一个创建队列的新方法,每次它使用 pop 从堆栈中删除并返回一个整数时,它都会将它添加到队列中。问题是while(!arrCopy.isEmpty())
似乎没有执行并且队列为空。这里有铸造问题吗?还是我向队列添加元素的方式有问题?
thanks!
谢谢!
edit:here is the code for my main function (which is the rest of my code):
编辑:这是我的主要功能的代码(这是我的其余代码):
public static void main(String[] args) {
Random rand = new Random();
Stack<Integer> a=new Stack<Integer>();
for (int i=0; i<10; i++){
a.push(rand.nextInt(50));
}
System.out.print("{ ");
while(!a.empty()){
System.out.print((int)a.pop() + ", ");
}
System.out.print("}");
reverse(a);
}
solution: i got it to work, thank you! the problem was that i was using pop to return (yet remove) all elements from the stack in order to print it before reversing it, which resulted in an empty stack. this is how i changed it to work!
解决方案:我让它工作了,谢谢!问题是我正在使用 pop 返回(但删除)堆栈中的所有元素,以便在反转之前打印它,这导致了一个空堆栈。这就是我改变它工作的方式!
public static Queue reverse(Stack arr){
Queue<Integer> arrCopy=new LinkedList<Integer>();
while(!arr.empty()){
arrCopy.add((int)arr.pop());
}
return arrCopy;
}
public static void main(String[] args) {
Random rand = new Random();
Stack<Integer> a=new Stack<Integer>();
for (int i=0; i<10; i++){
a.push(rand.nextInt(50));
}
System.out.println("List:");
System.out.println(a);
System.out.println("Reversed List:");
System.out.println(reverse(a));
}
回答by Sudhakar
Just reverse the stack order using below code. That revers the order.
只需使用以下代码反转堆栈顺序即可。这颠倒了顺序。
import java.util.Collections;
import java.util.Stack;
public class Test {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < 10; i++) {
stack.push(i);
}
System.out.println("Before reverse" + stack);
reverse(stack);
}
public static void reverse(Stack<Integer> arr){
arr.sort(Collections.reverseOrder());
System.out.println("After reverse");
System.out.println(arr);
}
}
Out put is:
输出是:
Before reverse[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After reverse
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
回答by khelwood
Here:
这里:
while(!a.empty()){
System.out.print((int)a.pop() + ", ");
}
System.out.print("}");
reverse(a);
You are emptying the stackbefore calling reverse
. If you keep popping elements off the stack until a.empty()
returns true, then you have emptied the stack, and you are passing an empty stack to the reverse
method.
您在调用之前清空堆栈reverse
。如果您一直从堆栈中弹出元素直到a.empty()
返回 true,那么您已经清空了堆栈,并且您正在将一个空堆栈传递给该reverse
方法。
Why not just use:
为什么不使用:
System.out.println(a);
reverse(a);
There's no need to pop all the elements off your stack in order to print them.
无需从堆栈中弹出所有元素即可打印它们。