我的堆栈推送方法不起作用,我不明白为什么。 Eclipse 说该参数不适用于我的 Stack 的原始类型。
Stack<Bracket> openingBrackets = new Stack<>();
for (int position = 1; position <= text.length(); ++position) {
char next = text.charAt(position - 1);
if ( next == '(' || next == '[' || next == '{') {
openingBrackets.push(next);
}
System.out.print(openingBrackets.pop());
}
我期待 (, [, { 添加到堆栈中
为了在 Java 中初始化或声明堆栈,您必须在胡萝卜中指定类型
<>
。
Java集合是通用的,这些括号用于定义内部存储的数据的类型。例如
Stack<Integer>
。看来你放了胡萝卜,但里面什么也没有,所以你有两个选择:
Stack openingBrackets = new Stack();
Stack<Character> openingBrackets = new Stack<Character>();