目前我正在尝试编写一个程序来创建一个MinHeap,尽管我似乎无法让ArrayList正常工作,因为其中一个类似乎无法与另一个类连接。当一个类试图将元素添加到ArrayList时,会出现问题。课程由----Java类和ArrayList
public interface QUEUE {
public Element getMin();
public void add(Element e);
}
public class Element {
public int key;
public Object data;
public Element(int i, Object o){
this.key = i;
this.data = o;
}
}
class TestProject {
public static void main(String[] args) {
System.out.println();
QUEUE q = new QUEUEHeap(10);
System.out.println(" 5, 1, 2, 33, -1, 3, 1, 2, 23, 13");
System.out.println();
q.insert(new Element(5,new Integer(5)));
q.insert(new Element(1,new Integer(1)));
q.insert(new Element(2,new Integer(2)));
}
import java.util.ArrayList;
public class QUEUEHeap implements QUEUE {
private ArrayList<Integer> q;
public PQHeap(int maxElements) {
q = new ArrayList<>(maxElements);
System.out.println("Element at index 1: " + q);
}
public Element getMin() {
}
public void insert(Element e) {
q.add(e.key,e.data); //e.key = i, e.data = o
}
}
新的错误分离:
no suitable method found for add(int,Obje
q.add(e.key,e.data); //e.key = i, e.data = o
^
method List.add(int,Integer) is not applicable
(argument mismatch; Object cannot be converted to Integer)
method AbstractList.add(int,Integer) is not applicable
(argument mismatch; Object cannot be converted to Integer)
method ArrayList.add(int,Integer) is not applicable
(argument mismatch; Object cannot be converted to Integer)
错误是来自插入方法的pq未定义。虽然我不知道如何解决这个问题。看来插入方法不能访问创建的ArrayList?请帮忙。 – Colour 2015-04-03 17:25:41
@Prashant然后没有这样的方法!准确的意思是什么?无论如何要让它像那样工作? – Colour 2015-04-03 17:29:56
@Prashant ['ArrayList#add(int,E)'](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-int-E-)exists 。这是将元素放在一个特定的索引 – 2015-04-03 17:33:42