Java 优先级队列通用

Java 优先级队列通用,java,generics,priority-queue,Java,Generics,Priority Queue,因此,我最近了解了泛型,并认为在优先级队列中实现它们会很酷。我有一个“Block”元素,它有一个名字和一个数据变量。优先级队列的节点由Block、Next和Prev组成 我附上下面的代码。我几乎只收到“应参数化”错误/警告。还有一个错误,它说我的“数据”元素无法解析为字段,这可能意味着我无法判断我想要块作为节点中的“元素E”。如有任何建议,我们将不胜感激 package QGen; public class Block<E> implements Comparable<Blo

因此,我最近了解了泛型,并认为在优先级队列中实现它们会很酷。我有一个“Block”元素,它有一个名字和一个数据变量。优先级队列的节点由Block、Next和Prev组成

我附上下面的代码。我几乎只收到“应参数化”错误/警告。还有一个错误,它说我的“数据”元素无法解析为字段,这可能意味着我无法判断我想要块作为节点中的“元素E”。如有任何建议,我们将不胜感激

package QGen;

public class Block<E> implements Comparable<Block<E>> {
    protected String firstName;
    protected int data;

    public Block(String firstName, int data) {
        super();
        this.firstName = firstName;
        this.data = data;

    }

    @Override
    public int compareTo(Block x) {
        return (this.data - x.data);
    }
}


package QGen;

public class PriorityQueue<E extends Comparable> {
    protected Node<E> firstSentinel;
    protected Node<E> lastSentinel;

    protected class Node<E> {
        protected Node<E> next;
        protected Node<E> prev;
        private E element;

        public Node(E e, Node<E> previous, Node<E> nextt) {
            element = e;
            prev = previous;
            next = nextt;
        }
    }

    public PriorityQueue() {
        firstSentinel = new Node<>(null, null, null);
        lastSentinel = new Node<>(null, null, null);
        firstSentinel.data = 11111;
        lastSentinel.data = 0;
        firstSentinel.prev = null;
        firstSentinel.next = lastSentinel;
        lastSentinel.prev = firstSentinel;
        lastSentinel.next = null;
    }

    public void enQueue(E x) {
        Node<E> newX = new Node<E>(x, null, null);
        if (firstSentinel.next == lastSentinel)// list is empyty
        {
            firstSentinel.next = newX;
            newX.prev = firstSentinel;
            newX.next = lastSentinel;
            lastSentinel.prev = newX;
        } else {
            Node<E> temp = newX;
            Node<E> curr = firstSentinel.next;
            while (curr != lastSentinel && temp.element.compareTo(curr) <= 0) {// <=comparison
                // replaced
                curr = curr.next;
            }
            Node<E> tempCurr = curr;
            temp.next = tempCurr;
            temp.prev = tempCurr.prev;
            tempCurr.prev.next = temp;
            tempCurr.prev = temp;

        }
    }

    public E deQueue() {
        if (firstSentinel.next == lastSentinel) {
            return null;
        } else {
            Node<E> temp = new Node<E>(null, null, null);
            temp = firstSentinel.next;
            firstSentinel.next = temp.next;
            temp.next.prev = firstSentinel;
            return temp.element;
        }
    }

    public void printt() {
        Node<E> temp = new Node<E>(null, null, null);
        temp = firstSentinel.next;
        while (temp != lastSentinel) {
            System.out
                    .println(temp.element.firstName + " " + temp.element.data);

            temp = temp.next;
        }
    }
}

package QGen;

public class containsMain<E> {

    public static void main(String[] args) {

        PriorityQueue<Block> example = new PriorityQueue<Block>();
        Block dequedObject = new Block<>(null, null);
        Block<Block> incomingName = new Block<>("r", 1);
        example.enQueue(incomingName);
        dequedObject = (Block) example.deQueue();

    }

}
封装QGen;
公共类块实现可比较{
受保护的字符串名;
受保护的int数据;
公共块(字符串名,整数数据){
超级();
this.firstName=firstName;
这个数据=数据;
}
@凌驾
公共整数比较(x区){
返回(this.data-x.data);
}
}
包装QGen;
公共类优先队列{
保护节点第一哨兵;
受保护的前哨节点;
受保护类节点{
受保护节点下一步;
受保护节点prev;
私人电子元件;
公共节点(E、上一个节点、下一个节点){
元素=e;
prev=先前的;
next=nextt;
}
}
公共优先级队列(){
firstSentinel=新节点(null,null,null);
lastSentinel=新节点(null,null,null);
firstSentinel.data=11111;
lastSentinel.data=0;
firstSentinel.prev=null;
firstSentinel.next=lastssentinel;
lastSentinel.prev=firstSentinel;
lastSentinel.next=null;
}
公共无效排队(E x){
Node newX=新节点(x,null,null);
if(firstSentinel.next==lastSentinel)//列表是临时的
{
firstSentinel.next=newX;
newX.prev=第一哨兵;
newX.next=lastSentinel;
lastSentinel.prev=newX;
}否则{
节点温度=newX;
节点curr=firstSentinel.next;

虽然(curr!=lastSentinel&&temp.element.compareTo(curr)没有查看方法的逻辑,但我对泛型有几点看法:

为什么块本身是泛型的?它不包含任何泛型字段,所以将其从块中删除

new Node<>(null, null, null);
// this is a bad idea, change it to new Node<E>(null, null, null)


firstSentinel.data = 11111;
lastSentinel.data = 0;

//Those two cant work, because firstSentinel is referencing a Node<E> and not a Block! Delete those two rows, as they make no sense in your generic implementation of the Queue

Block<Block> incomingName = new Block<>("r", 1); 
// This doesnt make sense, should be Block incomingName = new Block(...)
新节点(null,null,null);
//这是个坏主意,将其更改为新节点(null,null,null)
firstSentinel.data=11111;
lastSentinel.data=0;
//这两行行不通,因为firstSentinel引用的是节点而不是块!删除这两行,因为它们在队列的一般实现中没有意义
区块名称=新区块(“r”,1);
//这没有意义,应该是Block incomingName=new Block(…)