我的任务要求我从头开始实现一个通用优先级队列,但是我收到了一个我认为没有任何意义的错误。
public class PriorityQueue<E> {
private ArrayList<E> items = new ArrayList<E>(0);
...
public <E extends Comparable<E>> void insert(E newItem){
if(numOfItems == 0){
items.add(newItem); //ERROR: The method add(E) in the type ArrayList<E>
is not applicable for the arguments (E)
rear++;
numOfItems++;
}else{
//INCOMPLETE
}
}
}
最佳答案
public <T extends Comparable<E>> void insert(E newItem){
将第一个 'E' 更改为 'T',因为类型参数隐藏了原始的 'E'
关于java - 如何将通用项添加到通用 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20537120/