类型参数不在类型变量的范围内 [英] type argument is not within bounds of type-variable
问题描述
嗨堆栈溢出社区^_^
基本上,我正在使用堆栈并将中缀方程转换为后缀方程.这是我尝试编译 Stack 类时在屏幕上显示的错误消息:
Basically, I am working with Stacks and converting from Infix to Postfix equations. This is the error message showing on screen when I am trying to compile the Stack class:
Stack.java:5: error: type argument T#1 is not within bounds of type-variable T#2
private LinkedList<T> list;
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in class Stack
T#2 extends Comparable<T#2> declared in class LinkedList
我在试图找出这个错误时遇到了真正的麻烦,但遗憾的是我不知道可能是什么问题.如果我知道得更好一点,我可以为您提供更多信息.
I am having real troubles trying to figure out this error but sadly I don't have a clue of what might be the problem. If I knew a little better I could have provided you with more info.
预先感谢您的任何评论和帮助!
Thanks in advance for any comments and help!
更新:这是我的课...
Update: Here is my class...
package ListPkg;
public class Stack<T> // implements Comparable<Stack>>
{
private LinkedList<T> list;
public Stack()
{
this("list");
}
public Stack(String name)
{
list = new LinkedList(name);
}
public void push(T item)
{
list.insertAtFront(item);
}
public T pop()
{
list.removeFromFront();
}
public int lenghtIs()
{
return list.lengthIs();
}
public T peek()
{
return list.returnFirstNode();
}
public void print()
{
list.print();
}
public boolean isEmpty()
{
return list.isEmpty();
}
}
推荐答案
看来你有一个类 LinkedList
声明为
It seems you have a class LinkedList
declared as
class LinkedList<T extends Comparable<T>> {...}
但是您试图在声明为
class Stack<T> {
private LinkedList<T> list;
...
}
Stack
中声明的类型变量T
与LinkedList
中的类型变量T
完全无关.更重要的是,它们不兼容.LinkedList
期望一个类型是 Comparable
的子类型,但是 Stack
给它一个没有边界的类型参数.编译器不允许这样做.
The type variable T
declared in Stack
is completely unrelated to the type variable T
in LinkedList
. What's more, they are not compatible. LinkedList
expects a type that is a sub type of Comparable
, but Stack
is giving it a type argument that has no bounds. The compiler cannot allow this.
为您的 Stack
类的类型参数添加适当的边界
Add appropriate bounds to your Stack
class' type parameter
class Stack<T extends Comparable<T>> {
这篇关于类型参数不在类型变量的范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!