Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type argument is not within bounds of type-variable [duplicate]

Tags:

java

stack

Hi Stack Overflow community ^_^

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();
    }
}
like image 615
saqehi Avatar asked Mar 26 '14 02:03

saqehi


People also ask

What is a type argument?

What is a type argument? A reference type that is used for the instantiation of a generic type or for the instantiation of a generic method, or a wildcard that is used for the instantiation of a generic type .

What is type arguments in Java?

A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

What are bounds in Java?

A bound is a constraint on the type of a type parameter. Bounds use the extends keyword and some new syntax to limit the parameter types that may be applied to a generic type. In the case of a generic class, the bounds simply limit the type that may be supplied to instantiate it.

Which one of the following options is the correct name for empty type parameter?

It is called generics.


1 Answers

It seems you have a class LinkedList declared as

class LinkedList<T extends Comparable<T>> {...}

but you're trying to use it in a class Stack declared as

class Stack<T> {
    private LinkedList<T> list;
    ...
}

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.

Add appropriate bounds to your Stack class' type parameter

class Stack<T extends Comparable<T>> {
like image 143
Sotirios Delimanolis Avatar answered Nov 20 '22 12:11

Sotirios Delimanolis