【发布时间】:2011-11-05 03:56:10
【问题描述】:
我正在尝试通过一个类来实现 iSortableStack 接口。
这是我的主要功能,
public class SampleStack<E> {
E ch;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
ISortableStack<Character> s = new SortableStack<Character>();
SampleStack demo = new SampleStack();
while ((demo.ch == System.in.read()) != '\n')
if (!s.isFull())
s.push((Character) demo.ch);
while (!s.isEmpty())
System.out.print(s.pop());
System.out.println();
}
}
但我在这一行遇到一个错误,
while ((demo.ch == System.in.read()) != '\n')
错误:操作数类型 Object 和 int 不兼容
这里有什么问题?
【问题讨论】:
-
如果你将demo声明为
SampleStack<Character>? -
而不是抑制警告(就像你对
@SuppressWarnings("unchecked")所做的那样),你应该听从编译器的建议。如果您不理解该建议,那么您应该阅读直到您理解为止(这是您在此处询问所做的事情,这是一个很好的选择。)一般来说:除非您知道什么,否则不要忽略警告他们的意思。 -
除了术语上的矛盾之外,什么是可排序堆栈?
标签: java casting compiler-errors operators type-conversion