我正在尝试使用泛型实现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。我究竟做错了什么?
最佳答案
初始化类时需要使用具体类型
SkipNode<String> p1 = new SkipNode<String>(SkipNode.negInf, null);