java.util.Stack的迭代器中是否存在错误?
2022-09-01 01:06:44
今天,我试图在课堂上推动,然后使用来迭代(不使用pop)这些项目。我期待LIFO属性,但感到惊讶。java.util.Stack
Iterator
这是我尝试的代码。
import java.util.*;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
RobStack<Integer> rstack = new RobStack<Integer>(); // Correct Implementation
Stack<Integer> jstack = new Stack<Integer>(); // Default Java Implementation
rstack.push(0); jstack.push(0);
rstack.push(1); jstack.push(1);
rstack.push(2); jstack.push(2);
rstack.push(3); jstack.push(3);
System.out.print("Algo Stack: ");
for (int i : rstack)
System.out.print(i + " ");
System.out.print("\nJava Stack: ");
for (int i : jstack)
System.out.print(i + " ");
}
}
上述程序的输出如下:
Algo Stack: 3 2 1 0
Java Stack: 0 1 2 3
在上面的代码中,使用默认的Java实现,并使用Robert Sedgewick为他的算法类提供的实现。我发现Robert教授的实现工作正常,但实现失败了。jstack
rstack
java.util.Stack
这是一个错误还是设计使然?