Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Custom Java stack class (with generics) problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Custom Java stack class (with generics) problem

    Ok, so here are my classes and interfaces:

    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:

    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:

    LinkedStack<Integer> stack = new LinkedStack<Integer>();
    [...]
    System.out.println(this.iterableToString(stack));

    But I'm getting the following error:

    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.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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?

    public String iterableToString(Iterable<T> stack)

    iterableToString will accept any object that implements the Iterable<T> interface.

    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.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.
    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.

Similar Threads

  1. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 08:44 PM
  2. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 05:12 AM
  3. Stack Array..
    By qaromi in forum Collections and Generics
    Replies: 2
    Last Post: December 26th, 2009, 12:54 PM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  5. Error of "cannot access InToPost" in 3 and 5 code
    By jaysoncutie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 25th, 2009, 09:12 AM