所以我最近了解了Generics并认为在优先级队列中实现它们会很酷。我有一个“Block”元素,它有一个firstName和一个数据变量。优先级队列的节点由Block,Next和Prev。
组成我附上以下代码。我几乎完全得到“应该参数化”的错误/警告。并且一个错误表明我的“数据”元素无法解析为字段,这可能意味着我无法告诉我希望Block作为节点中的“元素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();
}
}
我知道我的PriorityQueue可能不是最好的实现,我会改进它。这是泛型,我无法提出解决方案
由于
答案 0 :(得分:0)
在不查看方法逻辑的情况下,我有几个关于泛型的评论:
为什么Block本身是通用的?它不包含任何通用字段,因此将其从Block中删除!
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(...)