Reputation: 21
I am working on an assignment to make a stack in java using Linked Lists, but there is an error I am getting, which prevents me from doing anything. here is first of all the Node class
public class SortedLinkedSetNode<T extends Comparable<T>>{
protected T value;
protected SortedLinkedSetNode<T> next;
// other methods
}
now here is the Stack class , of which I will only give the Push method because an error occurs there.
public class StackList<E> implements Stack<E> {
protected SortedLinkedSetNode upperMost;
public void push( E element) {
SortedLinkedSetNode <E> newNode = new SortedLinkedSetNode (element);
newNode.next = upperMost;
upperMost = newNode;
}
}
the problem is that when i try to make the newNode inside Push i get this message :
Bound mismatch: The type E is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type SortedLinkedSetNode<T>
I tried modifying the stack class so that it is
public class StackList<E extends Comparable<E>> implements Stack<E> {
but i am NOT allowed to modify it (thats just the assignment specification). I also tried using T not using anything and all sorts of combinations but there is always an error. I think that it all comes from this (which is given)
public class SortedLinkedSetNode<T extends Comparable<T>>{
but am not sure, and also not sure what to do if it really is from here
Can someone help me out with what exactly this is , and how i could solve it. If i can't make a new node in the stack class how can i even begin doing Push Pop etc.. ? Thank you
Upvotes: 0
Views: 469
Reputation: 178263
You have made your SortedLinkedSetNode
class generic, with an upper bound on T
. However, when you attempt to use the class with E
in StackList
, you didn't declare E
with the same upper bound, resulting in a bound mismatch. Try adding the upper bound to E
on StackList
:
public class StackList<E extends Comparable<E>> implements Stack<E> {
Also, the SortedLinkedSetNode
class is generic, but you are inconsistently applying a generic type parameter to your code that uses the class.
When declaring the variable upperMost
, use <E>
:
protected SortedLinkedSetNode<E> upperMost;
Additionally, add <E>
to when you create a new SortedLinkedSetNode
(could be the diamond "operator" <>
if you're using Java 1.7+).
SortedLinkedSetNode<E> newNode = new SortedLinkedSetNode<E> (element);
Upvotes: 1
Reputation: 25873
"The type E is not a valid substitute for the bounded parameter > of the type SortedLinkedSetNode" means that E
is more generic than T extends Comparable<T>
. You're declaring that your generic type T
must extend Comparable<T>
, while <E>
doesn't have such limitation, and as such, E
is invalid for your SortedLinkedSetNode<T extends Comparable<T>>
.
If you can't modify the Stack class, then you have no choice than to change your class to
public class SortedLinkedSetNode<T>
Also you need to use <E>
in your object instantiation
new SortedLinkedSetNode<E>(element);
Upvotes: 0