为什么这小段代码在第6行和第10行中给出非法的类型错误开始(用于循环)....我找不到任何不匹配的花括号...
class StackDemo{
final int size = 10;
Stack s = new Stack(size);
//Push charecters into the stack
for(int i=0; i<size; i++){
s.push((char)'A'+i);
}
//pop the stack untill its empty
for(int i=0; i<size; i++){
System.out.println("Pooped element "+i+" is "+ s.pop());
}
}
我已经实现了Stack类
最佳答案
您不能在类级别使用for
循环。将它们放在method
或block
中
同样,java.util.Stack
中的Java
没有这样的构造函数。
它应该是
Stack s = new Stack()
另一个问题
s.push(char('A'+i))// you will get Unexpected Token error here
只需将其更改为
s.push('A'+i);
关于java - 错误: illegal start of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26502235/