0

I'm pretty new to generics stuff. I'm experiencing "NodeListImpl is a raw type. References to generic type NodeListImpl should be parameterized" and have some question's about it, since i don't get all of it yet.

Code:

public class Node<T> {
    private String name;
    private T value;
    private NodeListImpl<Node<T>> nodes = new NodeListImpl<Node<T>>();

    public void addChild (Node<T> child) {
        this.nodes.add(child);
    }
}

public interface List<T> extends Iterable<T> {
    public abstract boolean contains(Object e);
    public abstract boolean add(T e);
    public abstract void addFirst(T e);
    public abstract T peekFirst();
    public abstract boolean isEmpty();
    public abstract T pollFirst();
    public abstract void clear();
}

public class ListImpl<T> extends LinkedList<T> implements List<T> {
}

public interface NodeList<Node> extends Iterable<Node> {
    public abstract boolean contains(Node e);
    public abstract boolean add(Node e);
    public abstract void addFirst(Node e);
    public abstract Node peekFirst();
    public abstract boolean isEmpty();
    public abstract Node pollFirst();
    public abstract void clear();
}

public class NodeListImpl<Node> extends LinkedList<Node> implements NodeList<Node> {

}

Task is:

  1. Write a List, that can handle to store and operatore with all types of data. Example: A list of integers, a list of class B, etc.

  2. Write a specialized version of that list, that can do the same, but only with Nodes.

Problems / Questions:

  1. Since our List needs to be able to work with ALL types of data, i thought we would make it List. Is that correct?

  2. Our NodeList only should be able to work with Nodes, so i made it NodeList. Is that correct?

  3. As told before, i am getting "NodeListImpl is a raw type. References to generic type NodeListImpl should be parameterized". I do understand that, Node itself has a generic Type since it needs to store a variable type of value. And when i say NodeListImpl it somehow blocks the view of Node. So i tried something like NodeListImpl> which doesn't work. What do i do wrong?

Thanks a lot!

2
  • Where do you get the warning from 3. Please state the line of that warning.
    – gexicide
    May 22, 2014 at 14:23
  • @ gexicide: i get that warning in "public interface NodeList<Node> extends Iterable<Node>".
    – user1648409
    May 22, 2014 at 14:51

1 Answer 1

0

Considering 1:

List<T> is fine.

Considering 2: Your NodeList class is wrong. class NodeList<Node>... declares a class NodeList with a type parameter named Node which will shadow your class Node. What you should write:

class NodeList<T extends Node>

then you have a class with a type parameter T, but T must be a subclass of Node.

Considering 3: Your node class is also a template class, so it also needs a parameter T, e.g. NodeList<T extends Node<T>>. But why is Node generic?

2
  • Node is generic, because Node needs to be able to save different kinds of values. String, Int, Class B etc. So i made it generic. Am i wrong here?
    – user1648409
    May 22, 2014 at 14:51
  • Btw: I don't want T to be a subclass of Node. What i want is that NodeList can only be a list of Nodes and Node itself can store all types of values in itself. Thats all...
    – user1648409
    May 22, 2014 at 14:53

Your Answer

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