- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Stack;
- @SuppressWarnings("unchecked")
- public class Test{
- public static void main(String[] args) {
- @SuppressWarnings("rawtypes")
- Stack stack = new Stack();
- stack.push("he");
- stack.push("saw");
- stack.push("a");
- stack.push("racecar");
- System.out.println(stack);
- reverseStack(stack);
- }
- @SuppressWarnings("rawtypes")
- private static void reverseStack(Stack stack) {
- Queue rev = new LinkedList();
- while(stack.size()>0){
- rev.offer(stack.pop());
- }
- while(rev.size()>0){
- stack.push(rev.poll());
- }
- System.out.println(stack);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1310201513785.html
来源: http://www.codesnippet.cn/detail/1310201513785.html