0

I'm trying to implement a LinkedList in java. The structure is simple: the controller (BList class), the node, the information component of the node. Now I would like to integrate the use of Generics: the information component of the node should be generic. I can't understand the following error. Why the compiler expects an Object type if I've specified a generic type <E>?

Test.java:7: error: cannot find symbol
        System.out.println(newNode.getElement().getUserId());   
                                               ^
  symbol:   method getUserId()
  location: class Object
1 error

This is my code. Thanks in advance for you help.

public class BList<E> {
    private Node head;

    public BList() {
        this.head = null;
    }

    public Node insert(E element) {
        Node<E> newNode = new Node<E>(element);

        newNode.setSucc(this.head);
        this.head = newNode;

        return newNode; 
    }
}

class Node<E> {
    private Node succ;

    private E element;

    public Node(E element) {
        this.succ = null;
        this.element = element;
    }

    public void setSucc(Node node) {
        this.succ = node;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public E getElement() {
//      return this.element; // ?
        return (E) this.element;
    }
}

class Element {
    private int userId;

    public Element(int userId) {
        this.userId = userId;
    }

    public int getUserId() {
        return this.userId;
    }
}      

public class Test {
    public static void main(String[] args) {

        BList<Element> bList = new BList<Element>();

        Node newNode = bList.insert(new Element(1));
// error happens here!      
        System.out.println(newNode.getElement().getUserId());

    }
}
1
  • Just a thought: you seem to be holding the Node class as package-private and the head-node as private in your BList implementation. So why are you returning the Node which was create in your insert method? Wouldn't that be to expose the internal representation of your BList? Commented Nov 30, 2015 at 8:14

1 Answer 1

4

You are using the raw form of Node in 2 places:

  1. The return type of BList's insert method. Return a Node<E> instead of a Node.
  2. The declaration of newNode in main. Declare it as Node<Element> instead of Node.

Because the raw form of Node was used, Object is now the return type, which doesn't have a getUserId method. Pre-generics code (before Java 1.5) would cast the result of getElement to an Element before calling getUserId, but making the above changes is the way to solve this problem with generics.

Sign up to request clarification or add additional context in comments.

5 Comments

insert should probably also return a Node<E> as well
@AmirAfghani: If you wanted to get pedantic, insert should be returning either a boolean to indicate its success, or nothing at all. For the example, though, you are quite correct.
@AmirAfghani I had the wrong method in item 1. Corrected. Thanks.
I agree @Makoto - I wasn't trying to be pedantic.
Thanks guys, i've fixed the code. You are right @Makoto, the add method of the official Java LikedList returns a boolean. Is this the right way to use generics in this context?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.