At the moment I'm trying to write a program to make a MinHeap, although I can't seem to get the ArrayList to work properly as one of the classes seem to not connect with another class. The problem occurs when one class is trying to add Elements to the ArrayList. Classes are separated by ----
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
}
}
New error:
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)
ArrayList#add(int, E)exists. It's to place the element at a specific indexArrayList#add(int, E)is the format used to talk about instance methods. It's not actual syntax