1. 最不推荐
LinkedList<Integer> stack1 = new LinkedList<>();
stack1.addLast(1);
stack1.addLast(2);
stack1.addLast(3);
while (!stack1.isEmpty()) {
System.out.println(stack1.pollLast());
}
System.out.println("=======");
2. Java 默认
Stack<Integer> stack = new Stack<>();
stack.add(1);
stack.add(2);
stack.add(3);
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("=======");
3. 如果能确定压栈数量,最优解
int[] stack2 = new int[3];标签:index,Java,System,效率,三种,out,stack2,stack,stack1 From: https://www.cnblogs.com/quzhongren/p/17231479.html
int index = 0;
stack2[index++] = 1;
stack2[index++] = 2;
stack2[index++] = 3;
while (stack2 != null && index == 0) {
System.out.println(stack2[--index]);
}