Custom Java stack class (with generics) problem
Ok, so here are my classes and interfaces:
Code :
public interface I_LinkedStack<T> extends Iterable<T>
public LinkedStack implements I_LinkedStack
-- private static class Node<T>
-- private class LinkedListIterator implements java.util.Iterator<T>
public Postfix<T>
Postfix is a class with the following methods:
Code :
public String iterableToString(Iterable<T> stack)
public String infixToPostfix(String infixExpression)
public int evaluatePostfix(String postfixExpression)
Everything is working fine with the exception of one thing. In my evaluatePostfix method I call iterableToString in order to print a stack declared locally within evaluatePostfix. Like this:
Code :
LinkedStack<Integer> stack = new LinkedStack<Integer>();
[...]
System.out.println(this.iterableToString(stack));
But I'm getting the following error:
Code :
iterableToString(java.lang.Iterable<T>) in Postfix<T> cannot be applied to (LinkedStack<java.lang.Integer>)
Why? LinkedStack is an iterable type, and Integer is just fine for T. It might also be worth noting that it compiles if I try to pass it String, Integer, or any other class like that.
Re: Custom Java stack class (with generics) problem
I am not familiar with the LinkedStack class. I am assuming this is the class you are using?
Code :
public String iterableToString(Iterable<T> stack)
iterableToString will accept any object that implements the Iterable<T> interface.
Code :
LinkedStack<Integer> stack = new LinkedStack<Integer>();
[...]
System.out.println(this.iterableToString(stack));
Here you are passing iterableToString a LinkedStack<Integer>. If LinkedStack<T> were declared to implement the Iterable<T> interface, you would be okay. However, the error you are getting suggests that LinkedStack<T> does not do this. Assuming that the link I included above is to the correct implementation of the LinkedStack, I can confirm this to be true. The only interface that LinkedStack<T> implements is Cloneable.
Re: Custom Java stack class (with generics) problem
Your first two lines didn't register with me earlier, you do show the definition of LinkedStack.
Quote:
public interface I_LinkedStack<T> extends Iterable<T>
public LinkedStack implements I_LinkedStack
LinkedStack implements I_LinkedStack, but that isn't the same thing as I_LinkedStack<T>. Is there a reason you dropped the type parameters? I suspect that is at the root of your error message.