i have the class BinaryTree:
public class BinaryTree<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root;
protected int size = 0;
private final Comparator<? super E> comparator;
/** Create a default binary tree */
public BinaryTree() {
comparator = null;
}
/** Create a binary tree from an array of objects */
public BinaryTree(E[] objects) {
for (int i = 0; i < objects.length; i++)
insert(objects[i]);
}
public BinaryTree(E[] objects, Comparator<E> c) {
for (int i = 0; i < objects.length; i++)
insert(objects[i]);
}
//some getters, setters, insert, search and etc...
}
我也有一个MyQueue类:
public class MyQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void enqueue(E e) {
list.addLast(e);
}
public E dequeue() {
return list.removeFirst();
}
public int getSize() {
return list.size();
}
public String toString() {
return "Queue: " + list.toString();
}
}
最后,我有了CreditCardTransaction类:
public class CreditCardTransaction {
private int cardNumber;
private String customerName;
private int amount;
public CreditCardTransaction(int cardNumber, String customerName, int amount) {
this.cardNumber = cardNumber;
this.customerName = customerName;
this.amount = amount;
}
public int getCardNumber() {
return cardNumber;
}
public void setCardNumber(int cardNumber) {
this.cardNumber = cardNumber;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
在主要部分中,我写了:
BinaryTree<MyQueue<CreditCardTransaction>> e = new BinaryTree<MyQueue<CreditCardTransaction>>();
i get the error:
Bound mismatch: The type MyQueue<CreditCardTransaction> is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type BinaryTree<E>
我尝试了各种类型的组合,但每次都出现这个错误,请帮帮我? 编辑 我将其添加到了MyQueue。
public class MyQueue<E> implements Comparable<E>{
@Override
public int compareTo(E o) {
// TODO Auto-generated method stub
return 0;
}
}
主要问题仍然是它给了我错误提示:
Bound mismatch: The type MyQueue<CreditCardTransaction> is not a valid
substitute for the bounded parameter <E extends Comparable<E>> of the
type BinaryTree<E>
MyQueue<E>
应该实现Comparable<MyQueue<E>>
以匹配BinaryTree
类型参数,而不是Comparable<E>
。 - Eran