I have created a priority Queue that holds songs with some attributes(id, title, likes). I want to print the heap, element by element on a format like this example:
5 ZZ TOP - La Grange 4167
instead it prints this:
[null, Song@330bedb4, Song@2503dbd3, Song@4b67cf4d, Song@7ea987ac... ]
This is my code:
public class PriorityQueue<T> {
private T[] heap;
private int size;
protected Comparator<T> cmp;
public PriorityQueue(int capacity, Comparator<T> cmp){
if(capacity < 1) throw new IllegalArgumentException();
this.heap = (T[]) new Object[capacity + 1];
this.size = 0;
this.cmp = cmp;
}
public void print(T[] heap) {
for (int i=1; i<=size; i++){
System.out.print(heap[i]);
}
System.out.println();
}
public void insert ( T ob){
if(ob == null) throw new IllegalArgumentException();
if(size == heap.length - 1)throw new IllegalArgumentException();
heap[++size] = ob;
swim(size);
}
}
somewhere on my main: (I put values in these variables)
Song s = new Song(id, title, likes);
System.out.println(s.getId() + " " + s.getLikes() + " " + s.getTitle());
pq.insert(s);
pq.print();