Colour
Colour

Reputation: 69

Java classes and ArrayList

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)

Upvotes: 0

Views: 861

Answers (1)

Vince
Vince

Reputation: 15146

Declare the ArrayList as a field variable, rather than a variable local to the constructor:

class MiniHeap {
    private ArrayList<Integer> pq;

    public MiniHeap(int maxElements) {
        pq = new ArrayList<>(maxElements);
    }

    //your methods..
}

Keep in mind that the constructor argument for ArrayList is not a maximum size; its an initial size. Lists don't have maximum sizes.


Element#data is of type Object. Since your ArrayList only accepts Integer, you are getting that error, hence the:

Object cannot be converted to Integer

Element#data should be the type as the ArrayList in your heap class. To allow variety in type, I'd personally give Element a type parameter:

class Element<T> {
   private int key;
   private T data;

   public Element(int key, T data) {
       this.key = key;
       this.data = data;
   }

   public T getData() {
       return data;
   }

   public int getKey() {
       return key;
   }
}

Then create a heap that allows a specific type of data (assign your list to that type):

class MiniHeap<T> {
   private ArrayList<T> pq;

   public MiniHeap(int initialAmount) {
       pq = new ArrayList<>(initialAmount);
   }

   public void insert(Element<T> element) {
       pq.add(element.getKey(), element.getData());
   }
}

Just simply declare the Element and MiniHeap with the same type argument:

public class Main {
    public static void main(String[] args) {
        Element<Integer> element = new Element<>(0, 10);
        MiniHeap<Integer> heap = new MiniHeap<>(20);

        heap.insert(element);
    }
}

Beware of auto-boxing. Primitive types cannot be used for generic type arguments, so primitive wrapper classes are used instead, which wraps the primitive value in an object. When uoi declare new Element<>(0, 10), the 10 is boxed into an object, since the constructor's parameters is (int, Integer) and not (int, int). To prevent auto-boxing, use new Integer(10) instead of just 10. Auto-unboxing could still be an issue though, depending on your situation.

Upvotes: 4

Related Questions