gpt4 book ai didi

Java : Bound mismatch: is not a valid substitute for the bounded parameter >

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:24 26 4
gpt4 key购买 nike

我有二叉树类:

    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>>();

我得到错误:

Bound mismatch: The type MyQueue<CreditCardTransaction> is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type BinaryTree<E>

我尝试了各种类型的混合,每当 eclipse 给我这个错误时,有什么帮助吗?

编辑

我添加到我的队列

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不执行 Comparable , 所以它不能替代 BinaryTree 的类型参数.

您可以删除 extends Comparable<E>绑定(bind)自 BinaryTree如果不需要,或更改 MyQueue实现Comparable<MyQueue<E>> .

关于Java : Bound mismatch: is not a valid substitute for the bounded parameter <E extends Comparable<E>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34737468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com