简体   繁体   English

实例化泛型类

[英]Instantiating generic class

I'm trying to implement a SkipList using generics and I have run into a problem. 我正在尝试使用泛型实现SkipList,但遇到了问题。 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); 错误发生在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); 我尝试删除该行上的<E>并将其更改为: 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. 我还尝试在参数中执行“ -oo”,但它希望我将Node构造函数参数更改为String和Integer。

What am I doing wrong? 我究竟做错了什么?

初始化类时需要使用具体类型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM