初学Stack,把书上示例原模原样抄下来,结果在import java.util.Stack和主方法第一句创建Stack里报错,不知为什么。有没有可能和我JDK版本有关
package chapter20;
import java.util.Stack;
public class Stack {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < 5; i++) {
stack.push(i);
}
System.out.println("isEmpty:"+stack.isEmpty());
System.out.println("capacity:"+stack.capacity());
System.out.println("peek:"+stack.peek());
// 集合遍历方式
for (Integer x : stack) {
System.out.println(x);
}
System.out.println("--------------------------");
// 栈弹出遍历方式
while (!stack.empty()) {
System.out.println(stack.pop());
}
}
}