Search code examples
javalistgenericsskip-lists

Instantiating generic class


I'm trying to implement a SkipList using generics and I have run into a problem. I can't seem to instantiate a node.

Node class:

public class SkipNode<E> 
{
    private E key;
    private Integer value;

    //Links
    private SkipNode<E> up, down, left, right;

    //Head and tail keys. Cannot add before -oo or after +oo.
    public static String negInf = "-oo";
    public static String posInf = "+oo";

    public SkipNode() {}

    public SkipNode(E k, Integer v)
    {
        key = k;                                // Add key
        value = v;                              // Assign value

        up = down = left = right = null;        // Set links to null
    }

    //Getters and setters down here
}

List class:

import java.util.Random;

public class SkipList<E>
{
    private SkipNode<E> head;
    private SkipNode<E> tail;

    private int size, height;
    private Random rand;

    public SkipList()
    {
        SkipNode<E> p1, p2;

        p1 = new SkipNode<E>(SkipNode.negInf, null);
    }
}

The error is on p1 = new SkipNode<E>(SkipNode.negInf, null); I tried removing the <E> on that line and changing it to this: p1 = new SkipNode(SkipNode<E>.negInf, null);, which doesn't make much sense but I thought I would try it. I also tried doing "-oo" in the parameter but it wants me to change the Node constructor parameters to String and Integer.

What am I doing wrong?


Solution

  • You need to use a concrete type when initializing the class

    SkipNode<String> p1 = new SkipNode<String>(SkipNode.negInf, null);