实例化泛型类

时间:2015-07-06 19:38:41

标签: java list generics skip-lists

我正在尝试使用泛型实现SkipList,但我遇到了一个问题。我似乎无法实例化一个节点。

节点类:

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
}

列表类:

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);
    }
}

错误发生在p1 = new SkipNode<E>(SkipNode.negInf, null);我尝试删除该行上的<E>并将其更改为:p1 = new SkipNode(SkipNode<E>.negInf, null);,这没有多大意义,但我想我会尝试一下。我也尝试在参数中执行“-oo”,但它希望我将Node构造函数参数更改为String和Integer。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

初始化类

时需要使用具体类型
SkipNode<String> p1 = new SkipNode<String>(SkipNode.negInf, null);